如何在 v4 中更新我的谷歌表? [英] How do I update my google sheet in v4?

查看:13
本文介绍了如何在 v4 中更新我的谷歌表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 google sheet v4 的新手,我只想知道如何在 v4 中更新我的 google sheet.我正在使用 Nodejs,以下是我使用的代码示例链接 方法:spreadsheets.values.update

I am new in google sheets v4 and I just want to know how can I update my google sheet in v4. I am using Nodejs and following is the code sample link which I am using Method: spreadsheets.values.update

推荐答案

您可以使用 链接.在您的情况下,结合 Quickstart 和示例脚本可能对您有用.示例脚本如下.

You can use the sample script of the link. In you case, combining Quickstart and the sample script may be useful for you. The sample script is as follows.

在这个示例脚本中,sample text的文本被导入到Sheet1的单元格a1中.

In this sample script, the text of sample text is imported to the cell a1 of Sheet1.

var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');

// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
    process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'sheets.googleapis.com-nodejs-quickstart.json';

// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
  if (err) {
    console.log('Error loading client secret file: ' + err);
    return;
  }
  // Authorize a client with the loaded credentials, then call the
  // Google Sheets API.
  authorize(JSON.parse(content), valuesUpdate);
});

/**
 * Create an OAuth2 client with the given credentials, and then execute the
 * given callback function.
 *
 * @param {Object} credentials The authorization client credentials.
 * @param {function} callback The callback to call with the authorized client.
 */
function authorize(credentials, callback) {
  var clientSecret = credentials.installed.client_secret;
  var clientId = credentials.installed.client_id;
  var redirectUrl = credentials.installed.redirect_uris[0];
  var auth = new googleAuth();
  var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, function(err, token) {
    if (err) {
      getNewToken(oauth2Client, callback);
    } else {
      oauth2Client.credentials = JSON.parse(token);
      callback(oauth2Client);
    }
  });
}

/**
 * Get and store new token after prompting for user authorization, and then
 * execute the given callback with the authorized OAuth2 client.
 *
 * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
 * @param {getEventsCallback} callback The callback to call with the authorized
 *     client.
 */
function getNewToken(oauth2Client, callback) {
  var authUrl = oauth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: SCOPES
  });
  console.log('Authorize this app by visiting this url: ', authUrl);
  var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });
  rl.question('Enter the code from that page here: ', function(code) {
    rl.close();
    oauth2Client.getToken(code, function(err, token) {
      if (err) {
        console.log('Error while trying to retrieve access token', err);
        return;
      }
      oauth2Client.credentials = token;
      storeToken(token);
      callback(oauth2Client);
    });
  });
}

/**
 * Store token to disk be used in later program executions.
 *
 * @param {Object} token The token to store to disk.
 */
function storeToken(token) {
  try {
    fs.mkdirSync(TOKEN_DIR);
  } catch (err) {
    if (err.code != 'EEXIST') {
      throw err;
    }
  }
  fs.writeFile(TOKEN_PATH, JSON.stringify(token));
  console.log('Token stored to ' + TOKEN_PATH);
}

function valuesUpdate(auth) {
  var sheets = google.sheets('v4');
  var request = {
    // The ID of the spreadsheet to update.
    spreadsheetId: 'my-spreadsheet-id',  // TODO: Update placeholder value.

    // The A1 notation of the values to update.
    range: 'Sheet1!a1:a1',  // TODO: Update placeholder value.

    // How the input data should be interpreted.
    valueInputOption: 'RAW',  // TODO: Update placeholder value.

    resource: {'values': [['sample text']]},

    auth: auth,
  };

  sheets.spreadsheets.values.update(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

    // TODO: Change code below to process the `response` object:
    console.log(JSON.stringify(response, null, 2));
  });
}

重要提示:

  • 请将上述脚本中的 my-spreadsheet-id 修改为您的.
  • 此示例脚本假设 Quickstart 的脚本运行正常.
  • 运行 Quickstart 脚本后,请删除 sheets.googleapis.com-nodejs-quickstart.json 一次,然后再运行上述示例脚本.删除文件后,请运行上述脚本.这样,将检索具有新范围的刷新令牌,并将其用于更新值.
  • 如果您想使用此脚本,请使用 googleapis v24 或更低版本.因为最新版本不行.因为会出现下面的错误,即使设置了valueInputOption.
    • 错误:'valueInputOption' 是必需的但未指定
    • 我相信这个错误会在不久的将来得到修正.
    • IMPORTANT :

      • Please modify my-spreadsheet-id to yours in above script.
      • This sample script supposes that the script of Quickstart works fine.
      • After run the script of Quickstart, please remove sheets.googleapis.com-nodejs-quickstart.json once, before run the above sample script. After remove the file, please run the above script. By this, the refresh token with the new scopes is retrieved and it is used for updating values.
      • If you want to use this script, please use googleapis v24 or less. Because the latest version doesn't work. Because the following error occurs, even if valueInputOption is set.
        • Error: 'valueInputOption' is required but not specified
        • I believe that this error will be modified in the near future.
        • 如果我误解了你的问题,我很抱歉.

          If I misunderstand your question, I'm sorry.

          这篇关于如何在 v4 中更新我的谷歌表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆