如何通过Google Drive Javascript OAuth2 API创建Google Sheets文档

How do I create a Google Sheets document via the Google Drive Javascript OAuth2 API?

本文关键字:Google 创建 Sheets API 文档 Javascript 何通过 Drive OAuth2      更新时间:2024-02-05

我正试图从基于浏览器的应用程序在Google Drive中创建一个空白的Google Sheets文档。我可以创建一个未知文件类型的空白文件,但无法创建Google Sheets文档。

我的代码如下:

function createfile() {
    var config = {
    'client_id': '<putyourclient_idhere>',
    'scope': 'https://www.googleapis.com/auth/drive'
    };
    gapi.auth.authorize(config, function() {
        var request = gapi.client.request({
            'path': '/upload/drive/v2/files',
            'method': 'POST'
        });
        var callback = function(file) {
            console.log(file)
        };
        request.execute(callback);

    });
}

我在这里查看了文件:https://developers.google.com/drive/v2/reference/files/insert

以及以下StackOverflow帖子:使用Drive API(Python)在Google Drive中创建空的电子表格如何创建一个空的谷歌文档/电子表格

我意识到我需要使用一个mimeType‘application/vnd.google-apps.电子表格’。

我还能够使用API资源管理器成功创建一个空白的Google Sheets文档,但无法确定这是如何工作的。它正在向发出帖子请求

请求有效载荷为:

{"title":"这是一个测试","mimeType":"application/vnd.google-apps.电子表格"}

但请注意,URL略有不同。

我有一个简单的方法可以用一个名为Cloud Snippets(cloudward.com)的产品来实现这一点。你可以创建这个片段并嵌入到你的网站中。

这个片段可以像一个命令一样简单:

<# create record for spreadsheet "create sheet example";
   set a to "";
   set b to "";
#>

这将创建一个具有名为"a"answers"b"的列的工作表

你实际上可以添加内容:

<# create record for spreadsheet "create sheet example";
   set Name to "Bob";
   set Relation to "Uncle";
   set Email to "bob@example.com";
#>