Your original code is already designed to update only the hyperlink address while leaving the cell's content (such as its displayed text or "name") unchanged. In ExcelScript, when you use cell.setHyperlink({ address: newAddress }), it updates the hyperlink's address but does not alter the cell's value or formula. This means the visible text in the cell (e.g., the display text of the hyperlink) should remain the same.
However, to make this explicit and ensure there's no unintended behavior (e.g., in cases where the cell might have a formula or other content), I've added a few safeguards:
Before updating the hyperlink, we'll capture the cell's current value and store it temporarily.
After setting the new hyperlink, we'll set the cell's value back to its original state. This ensures the cell's displayed text remains unchanged.
Here's the updated code:
typescript
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook.getActiveWorksheet();
for (let i = 1; i <= 116; i++) {
const cell = sheet.getRange(`A${i + 1}`);
const originalValue = cell.getValue(); // Capture the original cell value to preserve it
const hyperlink = cell.getHyperlink();
if (hyperlink && hyperlink.address) {
const oldAddress = hyperlink.address;
if (oldAddress.startsWith("\\\\fs02\\")) {
const newAddress = oldAddress.replace("\\\\fs02\\", "\\\\fs01\\");
// Set the new hyperlink address
cell.setHyperlink({ address: newAddress });
// Restore the original cell value to ensure it remains unchanged
cell.setValue(originalValue);
}
}
}
}
Key Changes and Explanation:
Capturing the Original Value: I added const originalValue = cell.getValue(); to store the cell's current value before making any changes. This includes any text, numbers, or formulas in the cell.
Restoring the Value: After cell.setHyperlink({ address: newAddress }), I added cell.setValue(originalValue); to explicitly set the cell's value back to what it was. This ensures that the cell's displayed content (e.g., the "name" or text) is not altered.
Why This Works: The setHyperlink method only affects the hyperlink property of the cell, not its core content. By restoring the value, we're being extra cautious to handle any edge cases, such as cells with formulas that might indirectly reference the hyperlink.
Notes:
Testing Recommendation: Run this updated code on a copy of your workbook to verify that the cell contents remain unchanged. You can add some logging (e.g., using console.log) to output the before-and-after values if needed for debugging.
Performance: This loop processes 116 cells, which is straightforward, but if your workbook is large, consider adding error handling or breaking it into smaller batches to avoid timeouts.
Potential Edge Cases: If any cells contain formulas (e.g., =HYPERLINK()), this code will preserve the cell's value, but it won't modify formulas directly. If you need to handle formulas specifically, let me know for further adjustments.
This should achieve what you described: the hyperlink address changes, but the cell's visible content stays the same. If you encounter any issues or need additional modifications, feel free to provide more details!