Remove hyperlinks from each cell in an Excel worksheet
This sample clears all of the hyperlinks from the current worksheet. It traverses the worksheet and if there is any hyperlink associated with the cell, it clears the hyperlink yet retains the cell value as is. Also logs the time it takes to complete traversal.
Note
This only works if the cell count is < 10k.
Setup: Sample Excel file
This workbook contains the data, objects, and formatting expected by the script.
Sample code: Remove hyperlinks
Add the following script to the sample workbook and try the sample yourself!
function main(workbook: ExcelScript.Workbook, sheetName: string = 'Sheet1') {
// Get the active worksheet.
let sheet = workbook.getWorksheet(sheetName);
// Get the used range to operate on.
// For large ranges (over 10000 entries), consider splitting the operation into batches for performance.
const targetRange = sheet.getUsedRange(true);
console.log(`Target Range to clear hyperlinks from: ${targetRange.getAddress()}`);
const rowCount = targetRange.getRowCount();
const colCount = targetRange.getColumnCount();
console.log(`Searching for hyperlinks in ${targetRange.getAddress()} which contains ${(rowCount * colCount)} cells`);
// Go through each individual cell looking for a hyperlink.
// This allows us to limit the formatting changes to only the cells with hyperlink formatting.
let clearedCount = 0;
for (let i = 0; i < rowCount; i++) {
for (let j = 0; j < colCount; j++) {
const cell = targetRange.getCell(i, j);
const hyperlink = cell.getHyperlink();
if (hyperlink) {
cell.clear(ExcelScript.ClearApplyTo.hyperlinks);
cell.getFormat().getFont().setUnderline(ExcelScript.RangeUnderlineStyle.none);
cell.getFormat().getFont().setColor('Black');
clearedCount++;
}
}
}
console.log(`Done. Cleared hyperlinks from ${clearedCount} cells`);
}
Training video: Remove hyperlinks from each cell in an Excel worksheet
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
Office Scripts