如何使用 google 驱动器 API 创建电子表格文件,并将默认选项卡标题设置为 "Sheet1" 以外的内容

How to create a spreadsheet file with the google drive API, and set the default tab title to something other than "Sheet1"

我想通过 google 驱动器 (v3) API 创建传播sheet 其中:

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

我整晚都在为 sheets(v4) 和 drive(v3) 爬取 google API,但仍然无法弄清楚这一点!

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

这是我要发送的 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, then use Method: spreadsheets.create 创建一个新的电子表格。使用像 Sheets API 这样的特定 API 可以为您提供更多专业化方法,例如添加 Sheet 标题的属性(对于更多工作表特定方法)。

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

导致:

这也将在您的 Google 驱动器中。

希望对您有所帮助。