Typescript 中的 HTTP Post 响应更新 google 电子表格

HTTP Post response in Typescript to update google spreadsheet

我正在从 google API 更新 spreadsheets url:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append

在更新表单中,我填写了以下属性: 在 spreadsheetId 字段中我输入了 spreadsheet 的 ID,在 range 字段中输入 sheet 的名称,在 insertDataOption 字段中我选择 INSERT_ROWS,在valueInputOption, RAW 和 Request body 字段中的以下 object:

{
  "values": [
    [
      "id",
      "ser",
      "IP",
      "host",
      "type",
      "auth"
    ],
    [
      "1",
      null,
      null,
      "",
      "Web",
      ""
    ],
    [
      "2",
      null,
      "191.174.230.02",
      "",
      "Proxy",
      ""
    ]
  ]
}

HTTP Post 响应如下: POST https://sheets.googleapis.com/v4/spreadsheets/[spreadsheetId-lex.europa.eu/values/[range-lex.europa.eu:append

我的问题:如何使用我要添加的属性和对象形成 HTTP Post 响应?也就是说,我如何告诉响应我想在 spreadsheet?

中添加哪些数据

如何使用节点将数据附加到电子表格

注意:这不使用 Typescript,但由于普通 JavaScript 也是有效的 Typescript,这应该适用于您的 Typescript 项目。

第一步

关注Node.js Quickstart。这将使您的项目设置为:

  • GCP 项目
  • API激活
  • OAuth 同意屏幕
  • 凭据 - 请务必下载并在项目文件夹中另存为 credentials.json

它还会让你安装 Node.js 库:

npm install googleapis

提出请求

以下代码改编自快速入门以使用 append 请求:

const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');

// =============
// AUTHORIZATION
// =============

const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];
const TOKEN_PATH = 'token.json';

fs.readFile('credentials.json', (err, content) => {
  if (err) return console.log('Error loading client secret file:', err);
  authorize(JSON.parse(content), append);
});

function authorize(credentials, callback) {
  const {client_secret, client_id, redirect_uris} = credentials.installed;
  const oAuth2Client = new google.auth.OAuth2(
      client_id, client_secret, redirect_uris[0]);

  fs.readFile(TOKEN_PATH, (err, token) => {
    if (err) return getNewToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    callback(oAuth2Client);
  });
}

function getNewToken(oAuth2Client, callback) {
  const authUrl = oAuth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: SCOPES,
  });
  console.log('Authorize this app by visiting this url:', authUrl);
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });
  rl.question('Enter the code from that page here: ', (code) => {
    rl.close();
    oAuth2Client.getToken(code, (err, token) => {
      if (err) return console.error('Error while trying to retrieve access token', err);
      oAuth2Client.setCredentials(token);
      fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
        if (err) return console.error(err);
        console.log('Token stored to', TOKEN_PATH);
      });
      callback(oAuth2Client);
    });
  });
}

// =============
//    APPEND
// =============

function append(auth){
    const sheets = google.sheets({version: 'v4', auth});
    sheets.spreadsheets.values.append({
        spreadsheetId: '13rdolwpUD4h4RTuExxxxxx', 
        range: 'Sheet1!A1', 
        valueInputOption: 'RAW',
        insertDataOption: 'INSERT_ROWS',  
        resource: {
            "values": [
                [
                  "id",
                  "ser",
                  "IP",
                  "host",
                  "type",
                  "auth"
                ],
                [
                  "1",
                  null,
                  null,
                  "",
                  "Web",
                  ""
                ],
                [
                  "2",
                  null,
                  "191.174.230.02",
                  "",
                  "Proxy",
                  ""
                ]
              ]
        },
    })
}

请务必将电子表格 ID 替换为您的 ID。

当您第一次使用 node . 运行 时,系统会提示您转到 URL 以获取 OAuth 流程的授权代码。一旦您获得授权,脚本就会执行。

参考