代码之家  ›  专栏  ›  技术社区  ›  Roni Jack Vituli

如何使用Python在Google Sheets的单个单元格中添加多个超链接?

  •  1
  • Roni Jack Vituli  · 技术社区  · 1 年前

    我正在编写一个Python脚本,使用Google Sheets API更新Google Sheets文档,我需要在一个单元格中添加多个超链接以及一些消息。然而,我很难做到这一点。以下是我要做的事情:

    我有一个Python脚本,可以用Google Sheets文档中的超链接更新特定单元格。超链接的格式应该是每个链接后面都有一条消息。例如,我希望单元格包含两个超链接,每个超链接都有一条不同的消息,我希望它们看起来像这样:

    1. 谷歌硬盘(超链接到“https://drive.google.com/“)

    2. 亚马逊驱动器(超链接到“https://www.amazon.com“)

    我曾尝试在单元格数据中使用HYPERLINK函数,并使用CHAR(10)插入换行符,但它并没有按预期工作。单元格显示文本,但链接不可单击。

    以下是我正在使用的Python代码:

    def list_problems(percentage_difference, asin, col_name, message="OK"):
            LIST_PROBLEMS = 'List Problems'
    
            worksheet = gc.open_by_key(spreadsheet_id).worksheet(LIST_PROBLEMS)
            cell = worksheet.find(asin)
            
            hyperlink_url1 = "https://drive.google.com/"
            hyperlink_url2 = "https://www.amazon.com"  # Replace with the Amazon link
            text1 = "Google Drive"
            text2 = "Amazon Drive"
            
            cell_format = {
                "backgroundColor": {
                    "red": 183/255,
                    "green": 225/255,
                    "blue": 205/255
                }
            }
            if percentage_difference >= 10:
                cell_data = (
                    f'=HYPERLINK("{hyperlink_url1}", "{text1}") & " | " & HYPERLINK("{hyperlink_url2}", "{text2}")'
                )
            else:
                cell_data = (
                    f'=HYPERLINK("{hyperlink_url1}", "{text1}") & " | " & HYPERLINK("{hyperlink_url2}", "{text2}")'
                )
    
            worksheet.update(f"{ColumnsRange[col_name]}{cell.row}", "", value_input_option='USER_ENTERED')  # Clear the cell
            worksheet.update(f"{ColumnsRange[col_name]}{cell.row}", cell_data, value_input_option='USER_ENTERED')
            
            # Apply cell formatting
            worksheet.format(f"{ColumnsRange[col_name]}{cell.row}", cell_format)
    

    以下是我运行代码时它在工作表中的样子。 谷歌工作表单元格不是超链接。。。

    enter image description here

    0 回复  |  直到 1 年前
        1
  •  1
  •   Tanaike    1 年前

    我相信你的目标如下。

    • 您希望将包含多个超链接的值放入Google电子表格的单元格中。
    • 您希望使用带有Python的gspread来实现这一点。

    在您的情况下,下面的示例脚本如何?

    示例脚本:

    在本示例中,使用gspread将该值放入Google电子表格上“Sheet1”的单元格“A1”中。

    import gspread
    
    client = # Please use your gspread client.
    spreadsheetId = "###" # Please set your Spreadsheet ID.
    sheetName = "Sheet1" # Please set your sheet name.
    
    # These values are from your showing script.
    hyperlink_url1 = "https://drive.google.com/"
    hyperlink_url2 = "https://www.amazon.com"
    text1 = "Google Drive"
    text2 = "Amazon Drive"
    cell_format = {"backgroundColor": {"red": 183/255, "green": 225/255, "blue": 205/255}}
    
    spreadsheet = client.open_by_key(spreadsheetId)
    sheet = spreadsheet.worksheet(sheetName)
    obj = [{"t": text1, "u": hyperlink_url1}, {"t": text2, "u": hyperlink_url2}]
    text = "\n".join([e["t"] for e in obj])
    requests = [
        {
            "updateCells": {
                "rows": [
                    {
                        "values": [
                            {
                                "userEnteredValue": {"stringValue": text},
                                "textFormatRuns": [{"format": {"link": {"uri": e["u"]}}, "startIndex": text.find(e["t"])} for e in obj],
                                "userEnteredFormat": cell_format
                            }
                        ]
                    }
                ],
                "range": {"sheetId": sheet.id, "startRowIndex": 0, "endRowIndex": 1, "startColumnIndex": 0, "endColumnIndex": 1},
                "fields": "userEnteredValue,textFormatRuns,userEnteredFormat"
            }
        }
    ]
    spreadsheet.batch_update({"requests": requests})
    

    测试:

    运行此脚本时,将获得以下结果。您可以在“A1”中看到包含多个超链接的单元格值。

    enter image description here

    参考文献: