【问题标题】:Google Sheets API v4 write dataGoogle Sheets API v4 写入数据
【发布时间】:2020-05-14 10:03:32
【问题描述】:

我正在尝试使用 Nodejs API v4 将数据写入 Google 表格。我可以成功读取和清除数据,所以我已经正确设置了所有内容。但是,使用文档here 中的示例作为更新方法,我无法弄清楚如何指定要放入 Google 工作表中的数据。我想要一个示例,说明如何将const data 保存的数据粘贴到指定的工作表中。这是我目前所拥有的:

const data = [
  [1, 2],
  [3, 4],
];
const auth = new google.auth.GoogleAuth({
  scopes: ["https://www.googleapis.com/auth/spreadsheets"],
});
const authClient = await auth.getClient();
const sheets = google.sheets({ version: "v4", auth: authClient });
const request = {
  spreadsheetId: "my-spreadsheet-id",
  range: "Sheet1!A:E",
  valueInputOption: "",
  resource: {},
  auth: authClient,
};
const response = await sheets.spreadsheets.values.update(request);

【问题讨论】:

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


    【解决方案1】:

    您必须指定一个值输入选项

    可能的值是:

    • USER_ENTERED
    • RAW
    • INPUT_VALUE_OPTION_UNSPECIFIED

    资源是你的数据数组。

    示例:

    const request = {
      spreadsheetId: "my-spreadsheet-id",
      range: "Sheet1!A:E",
      valueInputOption: "USER_ENTERED",
      resource: {values: [
        [1, 2],
        [3, 4],
      ]},
      auth: authClient,
    };
    

    【讨论】:

      【解决方案2】:

      添加到上面的 ziganotschka 的答案。看起来 request.resource 现在在 v4 as of around Feb 2020 中已经过时了,现在需要使用 request.requestBody 来代替。

      更新示例:

      const request = {
        spreadsheetId: "my-spreadsheet-id",
        range: "Sheet1!A:E",
        valueInputOption: "USER_ENTERED",
        requestBody: {values: [
          [1, 2],
          [3, 4],
        ]},
        auth: authClient,
      };
      

      【讨论】:

        猜你喜欢
        • 2017-10-10
        • 2016-10-13
        • 2018-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多