3

I'm looking at:

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create

and there does not appear to be a 'parents=[]' parameter in the request input parameter to create a google sheet.

sheets.spreadsheets.create(request, function(err, response) {
if (err) {
  console.error(err);
  return;
}

is it possible to specify the parent id of the folder I would like to create my google sheets in?

tehhowch
  • 9,645
  • 4
  • 24
  • 42
user1709076
  • 2,538
  • 9
  • 38
  • 59
  • Unfortunately, in the current stage, new Spreadsheet cannot be directly created in the specific folder using Sheets API, yet. So how about the following 2 patterns? 1. Create new Spreadsheet to the specific folder using Drive API. When you create new Spreadsheet using Drive API, please set the mimeType to ``application/vnd.google-apps.spreadsheet``. 2. After new Spreadsheet was created using Sheets API, it moves the Spreadsheet to the specific folder by a script. If I misunderstood your question, I apologize. – Tanaike Feb 28 '19 at 01:52
  • yeah i tried using the Drive API to create a file in a folder with the parents set (which does work) - however when i try to create a sheet in the folder i want (using the drive API) - and setting the mimeType to application/vnd.google-apps.spreadsheet it appears as though a binary file is created instead of a sheets file – user1709076 Feb 28 '19 at 03:04
  • Thank you for replying. I would like to confirm about ``it appears as though a binary file is created instead of a sheets file``. So can you provide the script for replicating your situation? – Tanaike Feb 28 '19 at 03:32

1 Answers1

2

sounds like when you create a sheet, the parentId is hard-coded to 'root' and there is nothing you can do about it yet. So therefore, after using the sheets api, you would then need to use the drive api to move the sheet (as suggested by player0).

Example NodeJS server code pasted below

var sheets = google.sheets('v4'); //cannot create in drive folder yet
let sconf  =
 { 
   resource:{properties:{title:name}},
   auth:auth
 };

 sheets.spreadsheets.create(sconf, function(e,d)
 {
   if(e)
     done({success:false})
   else
   {
     let fileId = d.data.spreadsheetId;
     let move   =
     {
       addParents:    [folderId],
       removeParents: 'root',
       fileId:        fileId
     };

     //move the sheet out of the root folder
     const drive = google.drive({ version: 'v3', auth });
     drive.files.update(move, function(e,d)
     {
       ....
     });
user1709076
  • 2,538
  • 9
  • 38
  • 59