-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathRemoveHyperlink.ts
More file actions
60 lines (56 loc) · 2.42 KB
/
Copy pathRemoveHyperlink.ts
File metadata and controls
60 lines (56 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* This sample clears all of the hyperlink from the current worksheet.
* It traverses through the worksheet and if there is any hyperlink associated with the cell, it cleares the hyperlink and retains the cell value as is.
*/
function main(workbook: ExcelScript.Workbook, sheetName: string = 'Sheet1') {
// Get the active worksheet.
let sheet = workbook.getWorksheet(sheetName);
const targetRange = sheet.getUsedRange(true);
if (!targetRange) {
console.log(`There is no data in the worksheet. `)
return;
}
console.log(`Target Range to clear hyperlinks from: ${targetRange.getAddress()}`);
const rowCount = targetRange.getRowCount();
const colCount = targetRange.getColumnCount();
const totalCells = rowCount * colCount;
if (totalCells > 10000) {
console.log("Too many cells to operate with. Consider editing script to use selected range and then remove hyperlinks in batches. " + targetRange.getAddress());
return;
}
// Call the helper function to remove the hyperlinks.
removeHyperLink(targetRange);
return;
}
/**
* Removes hyperlink for each cell in the target range. Logs the time it takes to complete traversal.
* @param targetRange Target range to clear the hyperlinks from.
*/
function removeHyperLink(targetRange: ExcelScript.Range): void {
const rowCount = targetRange.getRowCount();
const colCount = targetRange.getColumnCount();
console.log(`Searching for hyperliinks in ${targetRange.getAddress()} which contains ${(rowCount * colCount)} cells`);
let clearedCount = 0;
let cellsVisited = 0;
let groupStart = new Date().getTime();
for (let i = 0; i < rowCount; i++) {
for (let j = 0; j < colCount; j++) {
cellsVisited++;
if (cellsVisited % 50 === 0) {
let groupEnd = new Date().getTime();
console.log(`Completed ${cellsVisited} cells out of ${rowCount * colCount}. This group took: ${(groupEnd - groupStart) / 1000} seconds to complete.`);
groupStart = new Date().getTime();
}
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. Inspected ${cellsVisited} cells. Clearned hyperlinks in: ${clearedCount} cells`);
return;
}