I can create a spreadsheet using the following
fetch("https://sheets.googleapis.com/v4/spreadsheets?access_token=" access_token, {
method: 'POST', headers: { 'Accept': 'application/x-www-form-urlencoded' }
}).then(response => response.json()).then(json => {
console.log(json);
});
What are the parameters for naming that sheet and assigning it to a folder?
CodePudding user response:
Unfortunately, in the current stage, the Spreadsheet cannot be created to the specific folder while the Spreadsheet can be created with your expected title. In this case, Drive API is required to be used. When you want to create a new Spreadsheet with your expected title in the specific folder using Drive API, how about the following modification?
In this case, please enable Drive API, and please include a scope of https://www.googleapis.com/auth/drive
to your access token.
Modified script 1:
const access_token = "###"; // Please set your access token.
const folderId = "###"; // Please set your folder ID.
fetch("https://www.googleapis.com/drive/v3/files", {
method: 'POST', body: JSON.stringify({ name: "sample name", parents: [folderId], mimeType: "application/vnd.google-apps.spreadsheet" }), headers: { "Content-Type": "application/json", "Authorization": "Bearer " access_token }
}).then(response => response.json()).then(json => {
console.log(json);
});
- When this script is run, a new Spreadsheet with the title of "sample name" is created to the specific folder of
folderId
.
Modified script 2:
If you are required to use Sheets API, how about the following modification?
const access_token = "###"; // Please set your access token.
const folderId = "###"; // Please set your folder ID.
fetch("https://sheets.googleapis.com/v4/spreadsheets", {
method: 'POST', body: JSON.stringify({ properties: { title: "sample name" } }), headers: { "Content-Type": "application/json", "Authorization": "Bearer " access_token }
}).then(response => response.json()).then(json => {
console.log(json);
fetch("https://www.googleapis.com/drive/v3/files/" json.spreadsheetId "?removeParents=root&addParents=" folderId, {
method: 'PATCH', body: JSON.stringify({}), headers: { "Content-Type": "application/json", "Authorization": "Bearer " access_token }
}).then(response => response.json()).then(json => {
console.log(json);
});
});
- When this script is run, a new Spreadsheet with the title of "sample name" is created to the specific folder of
folderId
.
Note:
- If you want to create a new Spreadsheet to the folder of the shared drive, please add a query parameter of
supportsAllDrives=true
to the endpoint of Drive API.