node.js - 如何使用 Google Drive API 创建电子表格文件,并将默认选项卡标题设置为 "Sheet1"以外的其他内容

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

我想通过 Google Drive(v3) API 创建电子表格,其中:

  1. 我上传 CSV 数据以填充第一个选项卡
  2. 我可以设置 选项卡的名称不是“Sheet1”

我花了一整夜的时间爬行 Google API for Sheets(v4) 和 Drive(v3),但仍然无法弄清楚这个!

如果我不能这样做,似乎我将不得不发送额外的请求来更新工作表属性,以在完成初始上传后更改标题。如果可能的话,我想避免这种情况,但我意识到这可能是唯一的方法。

这是我发送的 API 请求:

let fileMetadata = {
  name: name,
  title: 'rawData', //This does NOT get set! Tab appears as "Sheet1"
  mimeType: 'application/vnd.google-apps.spreadsheet'
}

let media = {
  mimeType: 'text/csv',
  body: data // The body data is the raw parsed CSV
}

var Google = google.drive('v3')
Google.files.create({
  auth: auth,
  media: media,
  resource: fileMetadata
}, function(err, response) {
  if (err) {
    console.log('The API returned an error: ' + err)
    return done(err)
  } else {

    console.log('success!')
    console.log(response)

  }

})

最佳答案

我尝试使用Sheet API创建一个新的电子表格,然后使用方法:spreadsheets.create 。使用 Sheets API 等特定 API 为您提供更多专业方法,例如添加工作表标题属性(更多工作表特定方法)。

Creates a spreadsheet, returning the newly created spreadsheet.

// BEFORE RUNNING:
// ---------------
// 1. If not already done, enable the Google Sheets API
//    and check the quota for your project at
//    https://console.developers.google.com/apis/api/sheets
// 2. Install the Node.js client library by running
//    `npm install googleapis --save`

var google = require('googleapis');
var sheets = google.sheets('v4');

authorize(function(authClient) {
  var request = {
    resource: {
      // TODO: Add desired properties to the request body.
    },

    auth: authClient
  };

  sheets.spreadsheets.create(request, function(err, response) {
    if (err) {
      console.log(err);
      return;
    }

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

function authorize(callback) {
  // TODO: Change placeholder below to generate authentication credentials. See
  // https://developers.google.com/sheets/quickstart/nodejs#step_3_set_up_the_sample
  //
  // Authorize using one of the following scopes:
  //   'https://www.googleapis.com/auth/drive'
  //   'https://www.googleapis.com/auth/spreadsheets'
  var authClient = null;

  if (authClient == null) {
    console.log('authentication failed');
    return;
  }
  callback(authClient);
}

我使用了尝试此 API enter image description here

结果:

enter image description here

这也将位于您的 Google 云端硬盘中。

希望这有帮助。

关于node.js - 如何使用 Google Drive API 创建电子表格文件,并将默认选项卡标题设置为 "Sheet1"以外的其他内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43401836/

相关文章:

node.js - 如何在 express 中正确管理 CORS 策略?

node.js - AirTable API 通过邮件查找记录

google-apps-script - 使用谷歌应用程序脚本保护范围

javascript - 如何每周创建谷歌电子表格的备份?

java - 在不提示用户登录的情况下在服务器端验证 Google Drive API

javascript - Sequelize 和 Typescript 的外键

node.js - 版主删除消息命令

javascript - 谷歌脚本: Function to tally votes

google-apps-script - 如何将 Google Sheet 自定义功能作为插件

c# - Google Drive SDK System.Net.Http.Primitives 版本 1.5.0.0 而不是 2.1.10.0