0

I want to create a new spreadsheet in Google Drive, using the V4 REST API in Javascript. I can write data to an existing one, since I have an id, like this, once oauthed:

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.

2 Answers 2

1

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. So please modify it for your environment.

Reference :

If I misunderstand your question, I'm sorry.

Sign up to request clarification or add additional context in comments.

Comments

0

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:

For more information, see the Auth Guide.

For the sample code, see the example in the said documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.