【问题标题】:Search and change all links on the sheet. Google Sheets搜索并更改工作表上的所有链接。谷歌表格
【发布时间】:2023-02-02 17:43:36
【问题描述】:

这里有张桌子。桌子上有一张纸。包含 203,354 个单元格(12 列和 11962 行)。此工作表包含指向表中单元格的链接。链接看起来像这样:#gid=1282039879&range=A41,#gid=1282039879&range=A67:E67,#gid=1282039879&range=A5375:E5375,#gid=1282039879&range=A11780:E11780 等。很多链接,585 个。复制工作表后,#gid 更改并且链接不起作用。我们需要一个脚本来搜索工作表上的所有链接,确定工作表的当前#gid 并在链接中更改它。手动,这很容易通过“查找和替换”完成。但它需要自动化。该脚本将分配给按钮。 表格示例 - https://docs.google.com/spreadsheets/d/1iuPtdWyIo0SRbRrUzTp6hnP-iLn7B764G9j7w7jCkZE/edit?usp=sharing

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    您可以用 Python 编写脚本来自动执行此过程。为此,您可以使用 openpyxl 库读取 Excel 文件,找到工作表中的所有链接,提取当前 gid,并相应地更新链接。

    下面是一个示例代码,您可以将其用作起点:

    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")
    

    此脚本将打开 Excel 文件,找到所有带有链接的单元格,将旧的 gid 替换为当前的 gid,并使用不同的名称保存更新的文件。您可以进一步自定义此脚本以满足您的要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 2017-04-28
      • 2021-12-30
      • 2017-03-02
      相关资源
      最近更新 更多