Created
September 10, 2025 13:00
-
-
Save OndrejValenta/cc9769a55135dd0854791b1fa994d84c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This script converts all cells in selected range if they are of text value and the url is valid, check the isValid function if you need it updated. | |
You can add this script through Automate function. | |
``` | |
function main(workbook: ExcelScript.Workbook) { | |
// Get the currently selected range | |
let selectedRange = workbook.getSelectedRange(); | |
// Check if a range is actually selected | |
if (!selectedRange) { | |
console.log("No range selected. Please select cells containing URLs first."); | |
return; | |
} | |
// Get all values from the selected range | |
let values = selectedRange.getValues(); | |
let rowCount = selectedRange.getRowCount(); | |
let columnCount = selectedRange.getColumnCount(); | |
// Loop through each cell in the selected range | |
for (let row = 0; row < rowCount; row++) { | |
for (let col = 0; col < columnCount; col++) { | |
let cellValue = values[row][col]; | |
// Check if the cell contains text that looks like a URL | |
if (cellValue && typeof cellValue === 'string') { | |
let urlText = cellValue.toString().trim(); | |
// Basic URL validation - check if it starts with http/https or contains common URL patterns | |
if (isValidUrl(urlText)) { | |
// Get the specific cell | |
let cell = selectedRange.getCell(row, col); | |
const hyperlink: ExcelScript.RangeHyperlink = { | |
address: urlText, | |
screenTip: urlText, | |
textToDisplay: urlText | |
} | |
// Add hyperlink to the cell | |
cell.setHyperlink(hyperlink) | |
console.log(`Converted: ${urlText}`); | |
} else if (urlText.length > 0) { | |
console.log(`Skipped (not a valid URL): ${urlText}`); | |
} | |
} | |
} | |
} | |
console.log("URL conversion completed!"); | |
} | |
// Helper function to validate if text looks like a URL | |
function isValidUrl(text: string): boolean { | |
// Remove any extra whitespace | |
text = text.trim(); | |
if(!text?.length) return false; | |
// Check for common URL patterns | |
const urlPatterns = [ | |
/^https?:\/\//i, // Starts with http:// or https:// | |
/^www\./i, // Starts with www. | |
/\.[a-z]{2,}(\/|$)/i // Contains a domain extension | |
]; | |
// Check if text matches any URL pattern | |
return urlPatterns.some(pattern => pattern.test(text)); | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment