I have this code: function main(workbook: ExcelScript.Workbook) { const sheet =…
- User
I have this code:
function main(workbook: ExcelScript.Workbook) { const sheet = workbook.getActiveWorksheet();
for (let i = 1; i <= 116; i++) { const cell = sheet.getRange(`A${i + 1}`); const hyperlink = cell.getHyperlink(); if (hyperlink && hyperlink.address) { const oldAddress = hyperlink.address; if (oldAddress.startsWith("\\\\fs02\\")) { const newAddress = oldAddress.replace("\\\\fs02\\", "\\\\fs01\\"); cell.setHyperlink({ address: newAddress }); } } }}
Make it so that the cell names remain unchanged, the hyperlink link should change
Assistant
Here’s the updated code that ensures the cell names remain unchanged while only modifying the hyperlink addresses:
function main(workbook: ExcelScript.Workbook) { const sheet = workbook.getActiveWorksheet(); for (let i = 1; i <= 116; i++) { const cell = sheet.getRange(`A${i + 1}`); const hyperlink = cell.getHyperlink(); if (hyperlink && hyperlink.address) { const oldAddress = hyperlink.address; if (oldAddress.startsWith("\\\\fs02\\")) { const newAddress = oldAddress.replace("\\\\fs02\\", "\\\\fs01\\"); // Update only the hyperlink address without changing the cell text cell.setHyperlink({ address: newAddress, text: cell.getValue() as string // Preserve the original cell text }); } } } }Explanation:
-
Preserving Cell Text: The
setHyperlinkmethod updates both the hyperlink address and the cell text by default. To keep the cell text unchanged, we explicitly set thetextproperty to the current value of the cell usingcell.getValue() as string. -
Hyperlink Update: The hyperlink address is updated from
\\fs02\to\\fs01\as before, but now the cell's visible text remains the same.
This ensures the hyperlink is updated without altering the cell's displayed content.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.