Reputation: 3
There is a table. There is a sheet in the table. Contains 203,354 cells (12 columns and 11962 rows). This sheet has links to cells within the table. Links look like this: #gid=1282039879&range=A41, #gid=1282039879&range=A67:E67, #gid=1282039879&range=A5375:E5375, #gid=1282039879&range=A11780:E11780 etc. Many links, 585 pieces. After copying the sheet, the #gid changes and the links don't work. We need a script for Apps Script that searches for all links on the sheet, determines the current #gid of the sheet and changes it in the links. Manually, this is easily done through "Find and Replace". But it needs to be automated. This script will be assigned to the button. Table example - https://docs.google.com/spreadsheets/d/1iuPtdWyIo0SRbRrUzTp6hnP-iLn7B764G9j7w7jCkZE/edit?usp=sharing
Upvotes: 0
Views: 120
Reputation: 147
You can write a script in Python to automate this process. To do this, you can use the openpyxl library to read the Excel file, find all the links in the sheet, extract the current gid, and update the links accordingly.
Here's a sample code that you can use as a starting point:
import openpyxl
# Load the workbook
wb = openpyxl.load_workbook("file.xlsx")
# Select the sheet
sheet = wb["Sheet1"]
# Get the current gid of the sheet
gid = sheet.parent._gid
# Loop through each cell in the sheet
for row in sheet.iter_rows(values_only=True):
for cell in row:
if isinstance(cell, str) and "#gid=" in cell:
# Replace the old gid with the new one
cell = cell.replace(f"#gid=1282039879", f"#gid={gid}")
# Save the workbook
wb.save("file_new.xlsx")
This script will open the Excel file, find all the cells with links, replace the old gid with the current gid, and save the updated file with a different name. You can further customize this script to match your requirements.
Upvotes: 0