【问题标题】:Calling Background colors from google sheets using Google sheets api is missing data使用 Google 表格 api 从 google 表格调用背景颜色缺少数据
【发布时间】:2020-09-21 19:28:35
【问题描述】:

从此处链接的上一个问题 (Previous Question) 我了解了 Sheets.SpreadSheets.get 调用工作表数据的 JSON,这将允许我在我的项目中获取工作表的背景颜色。 Id 以前使用var BackgroundColors = ActiveWeekSheet.getDataRange().getBackgrounds(); 执行此操作,但被告知 JSON 方法将是一种更快的读/写方法。他们指示我阅读 Javascript 对象,但之后我仍然感到困惑。

我有以下代码。 TestArray = Sheets.Spreadsheets.get("1irmcO8yMxYwkcLaxZd1cN8XsTIhpzI98If_Cxgp1vF8"); 似乎调用带有工作表特定数据的 JSON。 TestArray 的记录器语句返回:testArrayObject: {"properties":{"gridProperties":{"rowCount":1000,"columnCount":26},"sheetType":"GRID","index":0,"sheetId":0,"title":"Awesome"}}

社区成员之前建议我可以在以下位置找到背景颜色:sheets[].data[].rowData[].values[].cellData.effectiveFormat.backgroundColor

我已将其中一个单元格突出显示为黄色,但在查看上述 JSON 时,我似乎找不到任何引用颜色的内容。绝对没有任何多层次的 JSON 可以引用 sheet->data->rowData->values->celldata.effectiveFormat.backgroundColor。

我在这里缺少什么?我需要以某种方式格式化吗?我没有调用正确的 JSON 开头吗?

谢谢!

【问题讨论】:

    标签: json google-apps-script google-sheets background-color google-sheets-api


    【解决方案1】:

    documentation所写,

    默认情况下,不会返回网格内的数据。您可以通过以下两种方式之一包含网格数据:

    • 使用 HTTP 中的 fields URL 参数指定列出所需字段的字段掩码
    Sheets.Spreadsheets.get(spreadsheetId, {
      ranges:"Sheet1!A1:A5",
      fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
    })
    
    • 将 includeGridData URL 参数设置为 true。如果设置了字段掩码,则忽略 includeGridData 参数
    Sheets.Spreadsheets.get(spreadsheetId, {
      ranges:"Sheet1!A1:A5",
      includeGridData: true
    })
    

    简而言之,

    • 多个不同的字段用逗号分隔,并且
    • 子字段以点分隔。
    • 为方便起见,同一类型的多个子字段可以在括号内列出。
    • 您可以测试 API here

    【讨论】:

    • 感谢您的回复。我能够使用你的方向来获取颜色信息。它返回这个:testArray:{"sheets":[{"data":[{"rowData":[{"values":[{"effectiveFormat":{"backgroundColor":{"red":1}}}]},{"values":[{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}}]},{"values":[{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}}]}]}]}]}. 我以为这会返回一个数组,但我似乎无法正常循环遍历它或得到它的.length。这是一个数组吗?
    • @LANCE 你需要学习解析对象和数组。阅读我之前要求您阅读的链接。这是一个对象,键为"sheets",值为arrays,它又包含多层嵌套数组和对象。要获得第一种颜色的红色值,您可以使用类似response.sheets[0].data[0].rowData[0].values[0].effectiveFormat.backgroundColor.red的内容,另请参阅 tag info page 以获取 JavaScript 指南。
    【解决方案2】:

    spreadsheets.get method 中有一些可选参数可以为您提供该数据,但您需要明确包含它们:

    • ranges – 要从电子表格中检索的范围。
    • includeGridData – 指定范围内的单元格数据。

    这指定了一个只有一个单元格的范围(Sheet1 中的 A1),但如果需要,您可以指定更大的范围并在数组中导航。

    var TestArray = Sheets.Spreadsheets.get(SS_ID, {ranges: "Sheet1!A1", includeGridData: true}); 
    

    请记住,这将返回带有 RGBA 的 Color object 非常重要 范围为 0-1 的值,但其他地方的应用程序脚本使用 hex colorconventional 0-255 RGB 值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多