简体   繁体   English

通过GDrive REST API用Javascript创建新的电子表格?

[英]Create new spreadsheet via GDrive REST API in Javascript?

I want to create a new spreadsheet in Google Drive, using the V4 REST API in Javascript. 我想使用Javascript中的V4 REST API在Google云端硬盘中创建一个新的电子表格。 I can write data to an existing one, since I have an id, like this, once oauthed: 我可以将数据写入到现有数据中,因为一旦获得授权,我就拥有这样的ID:

var accessToken=gapi.auth.getToken().access_token;
var str="https://sheets.googleapis.com/v4/spreadsheets/"+id+"/values/Sheet1!A1:E50?valueInputOption=USER_ENTERED";
var xhr=new XMLHttpRequest();
xhr.open("PUT",str);                                                                
xhr.setRequestHeader('Authorization','Bearer '+ accessToken);
xhr.send(JSON.stringify(data));

But I don't know how to create one from scratch in Javascript. 但是我不知道如何用Javascript从头开始创建一个。

I understood that you want to create new Spreadsheet. 我了解您想创建新的电子表格。 If my understanding is correct, how about this modification? 如果我的理解是正确的,那么该修改如何?

Sample script : 示例脚本:

var data = {"properties": {"title": "### filename of new spreadsheet ###"}}; // Added
var accessToken=gapi.auth.getToken().access_token;
var str="https://sheets.googleapis.com/v4/spreadsheets"; // Modified
var xhr=new XMLHttpRequest();
xhr.open("POST",str); // Modified
xhr.setRequestHeader('Authorization','Bearer '+ accessToken);
xhr.send(JSON.stringify(data));

Note : 注意 :

  • If the error related to scopes occurs, please check the reference. 如果发生与范围相关的错误,请检查参考。
  • data in this sample is very simple. 此样本中的data非常简单。 So please modify it for your environment. 因此,请根据您的环境进行修改。

Reference : 参考:

If I misunderstand your question, I'm sorry. 如果我误解了您的问题,对不起。

To create a new spreadsheet, you can follow this documentation . 要创建新的电子表格,您可以按照此文档进行操作

Creates a spreadsheet, returning the newly created spreadsheet. 创建一个电子表格,返回新创建的电子表格。

It requires one of the following OAuth scopes: 它需要以下OAuth范围之一:

For more information, see the Auth Guide . 有关更多信息,请参阅Auth Guide

For the sample code, see the example in the said documentation. 有关示例代码,请参见上述文档中的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM