I've figured out how to read values from a Budgeting Spreadsheet I created, but I can't figure out how to create a new spreadsheet with the Sheets API V4. I've been struggling with this problem for 5 months by now, has anyone solved this problem before?
Here's my code:
// READ - WORKING!
router.get("/get", (req, res) => {
var id = '1LoSF_4Z9aoiVvDsjFV9CMOd--vvz3fERfOPajVb2sv8';
var params = 'https://sheets.googleapis.com/v4/spreadsheets/?key='
var url = params + apiKey;
request(`https://sheets.googleapis.com/v4/spreadsheets/${id}?key=${apiKey}`, (error, response, body) => {
console.log("Body", body);
});
})
// Create - NOT WORKING!
router.post('/create', (req,res)=>{
request({
method: 'POST',
uri: `https://sheets.googleapis.com/v4/spreadsheets?fields=properties%2Ftitle&key=${apiKey}`
}, (error, response, body)=>{
console.log(body);
//Logs the body of the newly created spreadsheet
})
})
I used the guidelines from Google's API Explorer, you can find it here:
https://developers.google.com/apis-explorer/#p/sheets/v4/sheets.spreadsheets.create
Thank you!
How about this modification?
headers
using the access token. body
for giving the title of created spreadsheet. request({
method: 'POST',
uri: 'https://sheets.googleapis.com/v4/spreadsheets?fields=properties%2Ftitle%2CspreadsheetId',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken,
},
body: JSON.stringify({properties: {title: "sampleTitle"}}),
}, (error, response, body) => {
console.log(body);
});
https://www.googleapis.com/auth/spreadsheets
to the scopes. This scope is used to create the spreadsheet. I actually got it to work! Similar to the previous response (thank you Tanaike!!!).
request({
method: 'POST',
url: 'https://sheets.googleapis.com/v4/spreadsheets',
headers:{
'Authorization': 'Bearer (access token goes here)'
},
body: JSON.stringify({
properties: {
title: "Spreadsheet Title Goes Here"
}
})}, (error, response, body)=>{
if(!error && response.statusCode == 200){
var info = JSON.parse(body);
console.log(info);
} else {
console.log(error);
}
})
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.