简体   繁体   中英

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

I am trying to create a blank Google Sheets document in Google Drive from a browser based app. I am able to create a blank file with an unknown file type, but have not been able to create a Google Sheets document.

My code is as follows:

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);


    });
}

I have reviewed the documentation here: https://developers.google.com/drive/v2/reference/files/insert

And the following StackOverflow posts: Creating empty spreadsheets in Google Drive using Drive API (in Python) How to create an empty Google doc / Spreadsheet

I realise that I need to use a mimeType of 'application/vnd.google-apps.spreadsheet'.

I have also been able to successfully create a blank Google Sheets document using the APIs Explorer, but have been unable to determine how this is working. It is doing a post request to:

With a request payload of:

{ "title": "This is a test", "mimeType": "application/vnd.google-apps.spreadsheet" }

But note that the URL is slightly different.

I have an easy way to do this with a product called Cloud Snippets (cloudward.com). You can create the snippet and embed into your web site.

The snippet could be as easy as one command:

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

this creates a sheet with columns named "a" and "b"

you could actually add content:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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