【问题标题】:Get hyperlink to sheet based on SheetID根据 SheetID 获取工作表的超链接
【发布时间】:2021-05-25 08:43:44
【问题描述】:

我正在使用 API 通过 Python 导入 Smartsheet 报告。此报告中的一列包含一个可在 Smartsheet 中使用的超链接,但是当使用 Python 导入报告时,我只收到此列的文字,而不是它们后面的链接。是否有可能以任何其他方式获取这些超链接所引用的工作表的 URL?我在想可能基于 SheetID(我可以使用独立工作表的标题找到它),但非常欢迎所有其他建议!

【问题讨论】:

    标签: smartsheet-api


    【解决方案1】:

    我无法重现您描述的问题。

    我正在测试的报告包含以下数据。第一行中的 Google 链接是指向 https://www.google.com 的普通 URL,第二行中的 Contacts List 链接是指向 Smartsheet 中另一个工作表的工作表超链接。

    首先,我使用 Python SDK 获取报告,然后打印出第一行第二个单元格的内容(即包含Google 超链接的单元格):

    reportID = 6667768033503108
    
    report = smartsheet_client.Reports.get_report(reportID) 
    
    print(report.rows[0].cells[1])
    

    此代码的结果显示以下输出(此处为 JSON 格式以便于阅读):

    {
        "columnId": 5228827298293636,
        "displayValue": "Google",
        "hyperlink": {
            "url": "https://www.google.com"
        },
        "value": "Google",
        "virtualColumnId": 2581703205119876
    }
    

    所以,访问超链接的URL可以通过以下代码完成:

    url = report.rows[0].cells[1].hyperlink.url
    print(url)   #shows output: https://www.google.com
    

    同样的方法适用于获取第二行中工作表超链接的 URL。即,运行以下代码:

    reportID = 6667768033503108
    
    report = smartsheet_client.Reports.get_report(reportID) 
    
    url = report.rows[1].cells[1].hyperlink.url
    print(url)   #shows output: https://app.smartsheet.com/sheets/[ID]
    

    这种方法应该适合您,但如果由于某种原因您看到包含链接的单元格的 JSON 响应中的单元格对象(使用 Python SDK 时)实际上并不包含 hyperlink具有url 属性的对象——这可能表明 Python SDK 或底层 API 存在错误。在这种情况下,您可以尝试使用字典获取 URL 字符串,如以下代码所示。 (注意:您需要import json 才能使用此代码)。

    reportID = 6667768033503108
    
    # get the report
    report = smartsheet_client.Reports.get_report(reportID) 
    
    # load the contents of the second cell in the first row
    resp_dict = json.loads(str(report.rows[0].cells[1]))
    
    # read the url property from the dictionary
    url = resp_dict['hyperlink']['url']
    
    print(url)  #shows output: https://www.google.com
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 2018-04-22
      • 2019-01-16
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      相关资源
      最近更新 更多