【问题标题】:How to append rows with google sheet api v4 in nodejs如何在nodejs中使用google sheet api v4追加行
【发布时间】:2019-07-31 08:08:20
【问题描述】:

我真的不明白如何使用 apiv4 编写新行。

假设我有一个表,其中包含 ID、名称、计数、大小列。

官方的nodejs例子是这样的:

let values = [
  [
    // Cell values ...
  ],
  // Additional rows ...
];
let resource = {
  values,
};
this.sheetsService.spreadsheets.values.append({
  spreadsheetId,
  range,
  valueInputOption,
  resource,
}, (err, result) => {
  if (err) {
    // Handle error.
    console.log(err);
  } else {
    console.log(`${result.updates.updatedCells} cells appended.`);
  }
});

为此,我需要变量 valuesspreadsheetId范围valueInputOption

对于 value,它表示单元格值进入那里。这是否意味着它必须从左到右填充顺序?就像我的例子:

let values = [
      [
        "h7Bfhi87gBjh7fs9", //first the id
        "Max", //then name
        "20", //count
        "500" //size
      ]
    ];

这是正确的吗?

spreadsheetId 我已经有了。

对于最后两个变量,我不知道它们是什么意思。

首先是 范围 变量。对于读取,您必须为函数提供一个读取范围,这很简单。但是,如果我想将行追加到表中,为什么需要一个范围?

其次,我不知道 valueInputOption 做了什么。


tldr:

我需要向变量 valuesrangevalueInputOptions如果我想使用 google 电子表格 apiv4 将行附加到电子表格?

【问题讨论】:

    标签: node.js google-sheets google-sheets-api


    【解决方案1】:

    Values 是要进​​入单元格的数据数组。

    范围是数据需要到达的单元格的地址。

    valueInputOptions 可以是raw。见here


    已编辑:

    尝试替换:

    this.sheetsService.spreadsheets.values.append({
      spreadsheetId,
      range,
      valueInputOption,
      resource,
    }
    

    this.sheetsService.spreadsheets.values.append({
      spreadsheetId: 'string_spreadsheet_ID',
      range: 'Sheet1!A1:D1', // Or where you need the data to go 
      valueInputOption: 'RAW',
      resource: resource // takes the array created in the lines earlier
    }
    

    有关代码结构的提示,请参阅this page

    【讨论】:

    • 那么valueinputOption = ValueinputOptions.raw;?对于价值,我认为我在问题中的做法是正确的,但我仍然没有得到 Range 需要的内容,我只想将行追加到表的末尾。
    【解决方案2】:

    如果您只想在末尾追加行,您可以为范围指定“Sheet1”(或您要追加到的任何工作表名称)(参见https://developers.google.com/sheets/api/guides/values#appending_values) .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多