Node.js Google电子表格npm覆盖行而不是追加

Node.js Google电子表格npm覆盖行而不是追加,node.js,google-sheets-api,Node.js,Google Sheets Api,我正在使用,并且使用方法addRows()我想覆盖给定工作表上的现有行,但是由于某些原因,它一直在追加,我已经阅读了,甚至有一个插入选项,关于覆盖的方法,它不起作用 我还尝试使用行.delete()在写入之前删除每一行,但在删除其中一些行后,删除速度非常慢。我相信您的目标和当前情况如下 您希望将行覆盖到现有行。这是问题1 您希望以较低的处理成本删除多行。这就是问题2 您希望使用for Node.js实现上述功能 您已经能够使用Sheets API获取和输入Google电子表格的值 对问题1的

我正在使用,并且使用方法
addRows()
我想覆盖给定工作表上的现有行,但是由于某些原因,它一直在追加,我已经阅读了,甚至有一个插入选项,关于覆盖的方法,它不起作用


我还尝试使用
行.delete()
在写入之前删除每一行,但在删除其中一些行后,删除速度非常慢。我相信您的目标和当前情况如下

  • 您希望将行覆盖到现有行。这是问题1
  • 您希望以较低的处理成本删除多行。这就是问题2
  • 您希望使用for Node.js实现上述功能
  • 您已经能够使用Sheets API获取和输入Google电子表格的值
对问题1的答复: 在此示例脚本中,
spreadsheetId
第一页的第3行和第4行被
值覆盖

修改脚本:
  • 但是,在本例中,似乎每个循环都使用“spreadsheets.values.update”方法。而且,尽管我在寻找针对这种情况的batchUpdate方法,但不幸的是,我找不到它。因此,作为一种解决方法,我想提出以下示例脚本。在该脚本中,访问令牌从google电子表格的授权脚本中检索,并使用请求模块直接请求到spreadsheets.values.update方法的端点。因此,请添加
    const requests=require(“请求”)

    • 在这个示例脚本中,
      values
      被放入
      Sheet1!A3
      作为一个API调用的覆盖
对问题2的答复: 在此示例脚本中,将删除
电子表格ID
中的多行。不幸的是,在这种情况下,使用Sheets API每行删除一行。而且,尽管我在寻找针对这种情况的batchUpdate方法,但不幸的是,我也找不到它。因此,作为一种解决方法,我想提出以下示例脚本。在该脚本中,访问令牌从google电子表格的授权脚本中检索,它直接向spreadsheets.batchUpdate方法的端点发出请求

示例脚本: 在此示例脚本中,通过一次API调用删除
电子表格ID
的工作表ID
0
中的第3行和第4行

const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const requestBody = {
  requests: [
    {
      deleteDimension: {
        range: {
          sheetId: 0, // Please set the sheet ID.
          dimension: "ROWS",
          startIndex: 2, // Please set the start index of row you want to delete.
          endIndex: 4, // Please set the end index of row you want to delete.
        },
      },
    },
  ],
};

const doc = new GoogleSpreadsheet(spreadsheetId);
await doc.useServiceAccountAuth(creds);
requests.post(
{
  url: `https://sheets.googleapis.com/v4/spreadsheets/${doc.spreadsheetId}:batchUpdate`,
  headers: {
    "content-type": "application/json",
    Authorization: `Bearer ${doc.jwtClient.credentials.access_token}`,
  },
  body: JSON.stringify(requestBody),
},
(err, res, body) => {
  if (err) {
    console.log(err);
    return;
  }
  console.log(body);
});
参考资料:

您好,感谢您的回复,实际上我更倾向于从第3点开始使用google spreedsheet包装表单npm。最后,我通过执行
spreadsheet.clear()
,然后执行
spreadsheet.setHeaderRow()
复制标题,然后执行
spreadsheet.addRows()
@lvaro来修复它。谢谢您的回复。我不得不为我糟糕的英语水平道歉。从你的答复中,我无法理解你当前的问题,无法得到我的答复。我再次为此道歉。如果我的回答有问题,你能告诉我吗?我想修改它们。您的答案似乎是直接使用谷歌工作表API,而我使用的是谷歌电子表格npm包装器@Álvaro感谢您的回复。对我明白了。所以在我的回答中,我提出了使用“谷歌电子表格”的脚本。但是,现在看来,为了降低脚本的处理成本,“GoogleSpreadsheet”没有使用批处理请求。因此,作为附加建议,我添加了使用Sheets端点API的示例脚本。我认为也可以使用Node.js的googleapis。但我不确定你是否能用googleapis。所以我建议直接向端点请求。如果我的补充建议对你的情况没有帮助,我道歉。
  const spreadsheetId = "###"; // Please set the Spreadsheet ID.
  const values = [["A1", "B1", "C1"], ["A2", "B2", "C2"]]; // Please set the values you want to overwrite.
  const overwriteRange = "Sheet1!A3"; // Please set the 1st range you want to overwrite as a1Notation.

  const doc = new GoogleSpreadsheet(spreadsheetId);
  await doc.useServiceAccountAuth(creds);
  requests.put(
  {
    url: `https://sheets.googleapis.com/v4/spreadsheets/${doc.spreadsheetId}/values/${overwriteRange}?valueInputOption=USER_ENTERED`,
    headers: {
      "content-type": "application/json",
      Authorization: `Bearer ${doc.jwtClient.credentials.access_token}`,
     ,
    body: JSON.stringify({ values: values }),
  },
  (err, res, body) => {
    if (err) {
      console.log(err);
      return;
    }
    console.log(body);
  });
const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const requestBody = {
  requests: [
    {
      deleteDimension: {
        range: {
          sheetId: 0, // Please set the sheet ID.
          dimension: "ROWS",
          startIndex: 2, // Please set the start index of row you want to delete.
          endIndex: 4, // Please set the end index of row you want to delete.
        },
      },
    },
  ],
};

const doc = new GoogleSpreadsheet(spreadsheetId);
await doc.useServiceAccountAuth(creds);
requests.post(
{
  url: `https://sheets.googleapis.com/v4/spreadsheets/${doc.spreadsheetId}:batchUpdate`,
  headers: {
    "content-type": "application/json",
    Authorization: `Bearer ${doc.jwtClient.credentials.access_token}`,
  },
  body: JSON.stringify(requestBody),
},
(err, res, body) => {
  if (err) {
    console.log(err);
    return;
  }
  console.log(body);
});