【问题标题】:Are cell notes and color queryable with gspread?gspread 是否可以查询单元格注释和颜色?
【发布时间】:2016-10-22 05:57:13
【问题描述】:

我正在尝试学习 Python 和这个 Google API,我想知道单元格注释和颜色是否可以在 gspread 或其他形式中查询?如果是这样,有人可以为我指出正确的方向或文档吗?

到目前为止,我发现了两件看起来可以提供帮助的东西,但我并不真正了解如何将其中的任何内容用于 Python,或者只是使用什么。

Google Apps Scripts - Accessing Cell Notes and Comments

https://github.com/burnash/gspread/issues/50

【问题讨论】:

    标签: python google-sheets google-spreadsheet-api gdata gspread


    【解决方案1】:

    如果您点击了 GitHub 问题的链接,您会看到 setting 在 GSpread 3.7 中解决了注释,但省略了获取(或者我太密集而无法找到它)。因为我需要,所以我使用 Google service 对象编写了我自己的。假设您已设置服务帐户凭据(与 gspread 相同),请创建您的 service

    from oauth2client.service_account import ServiceAccountCredentials
    from googleapiclient import discovery
    
    scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
    creds = ServiceAccountCredentials.from_json_keyfile_name(filename, scope)
    service = discovery.build('sheets', 'v4', credentials=creds)
    

    使用service 和电子表格id 获取注释:

    fields = 'sheets/data/rowData/values/note'
    ranges = ["'Sheet1'!A1")]
    request = service.spreadsheets().get(spreadsheetId='1IDDMQqAV5t4Rqblahblah', ranges=ranges, fields=fields)
    note_json = request.execute()
    try:
        note = note_json['sheets'][0]['data'][0]['rowData'][0]['values'][0]['note']  # <-- there's got to be a better way
    except KeyError:
        note = None
    print(note)
    

    这独立于gspread 模型,所以它不是很好,但它可以工作。对于以更好的方式从 note_json 提取笔记的反馈,我将不胜感激。

    【讨论】:

      【解决方案2】:

      我认为,如果您首先了解 Python 如何与 Google 电子表格一起工作,将会很有帮助。 这是电子表格的Python Quickstart。 确保您还安装了Google Data Python Library

      有一个getBackground() 可能对您有用。它返回范围内左上角单元格的背景颜色(即“#ffffff”)。不过,请尝试在 Python 中找到等价物。

      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheets()[0];
      
      var cell = sheet.getRange("B5");
      Logger.log(cell.getBackground());
      

      希望这会有所帮助。

      【讨论】:

      • 您好,感谢您的回复。我的理解是,所提供的链接适用于 Google Sheets v3 的 Gdata,并且正在查看 Google Sheets v4(发现服务 API)?这或多或少是正确的?我不知道这意味着什么,是否意味着您仍然可以使用这个 gdata 库还是需要安装其他东西?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多