繁体   English   中英

使用 userEnteredValue 作为 object 在工作表上批量更新

[英]BatchUpdate on Sheets with userEnteredValue as an object

希望向 BatchUpdate 提供请求 object,以便可以更新单元格值和格式。 我正在构建以下请求 object:

   const request = [{
    updateCells: {
      range: {
        sheetId:grid.sheetId,
        startRowIndex: grid.startRow,
        endRowIndex: grid.endRow,
        startColumnIndex: grid.startCol,
        endColumnIndex:grid.endCol
      },
      rows: [
        {
          values: [
          {
            userEnteredFormat: {
              backgroundColor: {
                red: 1,
                green: 0.4,
                blue: 0.4
              }
            },
            userEnteredValue: {
              stringValue: {object containing the row}
            }
          }
        ]
      }
    ],
    fields: "userEnteredFormat,userEnteredValue"
   }
   }];

显然我收到错误消息“在标量场上启动 object”。 是否有一些不同的请求类型来提供行作为数组或 object? 查看文档 [https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/sheets#RowData],它看起来不像。

TIA

虽然不幸的是,我不确定我是否能正确理解{object containing the row} of stringValue: {object containing the row}在你的请求正文中,如果{object containing the row} of stringValue: {object containing the row}是一个像{key: "value"}, an error like occurs. I thought that this might be the reason for your current issue. And also, when an array like occurs. I thought that this might be the reason for your current issue. And also, when an array like occurs. I thought that this might be the reason for your current issue. And also, when an array like is used, an error like occurs. In this case, please use a string value like occurs. In this case, please use a string value like 请注意这一点。

从你的问题Is there some different request type to be made to provide the row as an array or object? ,如果你想使用一个包含值的数组到这个请求体,下面的修改怎么样?

修改脚本:

function myFunction() {
  const spreadsheetId = "###"; // Please set your Spreadsheet ID.
  const grid = { sheetId: 0, startRow: 0, startCol: 0 }; // Please set your gridrange.
  const values = [["sample value1", "sample value2"], ["sample value3", "sample value4"]]; // Please set your values as 2 dimensional array.

  const request = [{
    updateCells: {
      range: {
        sheetId: grid.sheetId,
        startRowIndex: grid.startRow,
        startColumnIndex: grid.startCol,
      },
      rows: values.map(r => ({
        values: r.map(c => ({
          userEnteredFormat: { backgroundColor: { red: 1, green: 0.4, blue: 0.4 } },
          userEnteredValue: { stringValue: c }
        }))
      })),
      fields: "userEnteredFormat,userEnteredValue"
    }
  }];
  Sheets.Spreadsheets.batchUpdate({ requests: request }, spreadsheetId);
}
  • 运行此脚本时, values将从sheetId中的单元格“A1”中放入。

  • 关于grid ,在这种情况下,我认为当开始单元格坐标(左上角)被放置时,可以使用这个脚本。

笔记:

  • 这是一个使用数组的简单示例脚本。 因此,如果您的values包括数字、boolean 和公式,请使用numberValueboolValueformulaValue键。 请注意这一点。 而且,如果您使用的是 JSON object,当您将其转换为二维数组时,您可以使用此脚本使用它。

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM