同时在Node and Google Sheets v.4中批量更新单元格值和背景色

问题描述 投票:0回答:1

我正在尝试使用Google Sheets v4 api更新单元格的背景颜色,但仍在运行。我可以在每次迭代时更新单元格的值,但是当我尝试更改背景色时,

"cell": {
                            "userEnteredFormat": {
                                "backgroundColor": {
                                    "red": 1.0,
                                    "green": 0.4,
                                    "blue": 0.4
                                }
                            }
                        },

在下面的代码中,我不断收到错误-GaxiosError: Invalid JSON payload received. Unknown name "cell" at 'data[0]': Cannot find field.

                function updateClientFeedbackCells(auth) {
                    var sheets = google.sheets({ version: 'v4' });

                    authorize(function () {
                        var request = {
                            spreadsheetId: process.env['GOOGLE_SHEET_ID_' + envString],  // 
                            valueInputOption: 'RAW',
                            resource: {

                                "data": [
                                    {   
                                        "range": "N" + ucfSelectedRowIndex,
                                        "majorDimension": "COLUMNS",
                                        "cell": {
                                            "userEnteredFormat": {
                                                "backgroundColor": {
                                                    "red": 1.0,
                                                    "green": 0.4,
                                                    "blue": 0.4
                                                }
                                            }
                                        },

                                        "values": [
                                            [
                                                true
                                            ],
                                        ]
                                    },
                                ],
                            },
                            auth,
                        };
                        sheets.spreadsheets.values.batchUpdate(request, function (err, response) {
                            if (err) {
                                console.error(err);
                                return;
                            }
                            else {
                                console.info(response);
                                console.log("Client Feedback Sent Col Values updated");
                            };
                        });
                    });
                    function authorize(callback) {
                        //   'https://www.googleapis.com/auth/drive'
                        //   'https://www.googleapis.com/auth/drive.file'
                        //   'https://www.googleapis.com/auth/spreadsheets'
                        var auth = "https://www.googleapis.com/auth/drive";
                        if (auth == null) {
                            console.log('authentication failed');
                            return;
                        }
                        callback(auth);
                    }
                }

我已经阅读了以下文档,但由于有太多不同的信息,因此似乎无法弄清楚我在做什么。请问,我的范围,api类型或语法有问题吗?

javascript node.js google-sheets-api
1个回答
0
投票
  • 您想使用batchUpdate方法通过一个API调用来更新单元格的值和背景。
    • 您要更新“ N”列的单元格。行号由ucfSelectedRowIndex给出。
  • 您想使用Node.js的googleapis实现此目的。
  • 您已经能够通过Sheets API获取和放置Spreadsheet的值。

如果我的理解是正确的,那么这个答案呢?

修改点:

  • 为了使用batchUpdate方法通过一个API调用来更新单元格的值和背景,请使用Sheets API的电子表格.batchUpdate方法。这与电子表格.values.batchUpdate的方法不同。
  • [当使用电子表格.batchUpdate的方法时,请使用the gridrange作为范围而不是a1Notation。

修改的脚本:

在此修改后的脚本中,我修改了API调用的脚本。使用此脚本之前,请设置电子表格ID和工作表ID。并且请设置ucfSelectedRowIndex的值。

const sheets = google.sheets({ version: "v4" });
const spreadsheetId = "###";
const sheetId = "###"; 
const ucfSelectedRowIndex = 1; // Please set more than 0.
const request = {
  requests: [
    {
      updateCells: {
        range: {
          sheetId: sheetId,
          startRowIndex: ucfSelectedRowIndex - 1,
          endRowIndex: ucfSelectedRowIndex,
          startColumnIndex: 13, // Column "N"
          endColumnIndex: 14 // Column "N"
        },
        rows: [
          {
            values: [
              {
                userEnteredFormat: {
                  backgroundColor: {
                    red: 1,
                    green: 0.4,
                    blue: 0.4
                  }
                },
                userEnteredValue: {
                  boolValue: true
                }
              }
            ]
          }
        ],
        fields: "userEnteredFormat,userEnteredValue"
      }
    }
  ]
};
sheets.spreadsheets.batchUpdate(
  { spreadsheetId: spreadsheetId, requestBody: request, auth },
  function(err, response) {
    if (err) {
      console.error(err);
      return;
    } else {
      console.info(response);
      console.log("Client Feedback Sent Col Values updated");
    }
  }
);
  • 运行脚本时,将修改单元格“ N1”的值和背景。

参考:

如果我误解了您的问题,而这不是您想要的结果,我深表歉意。

© www.soinside.com 2019 - 2024. All rights reserved.