alpal24
alpal24

Reputation: 23

Saving a copy of a sheet to a specific folder

On the trigger of a form submission, I have created a copy of a sheet. I can't figure out how to save that copied sheet in a specific folder. Once the copy is created, I've tried calling the folder then saving the copied sheet to that folder.

//Copying the sheet (triggered by a form submission)

var copyOfSheet = SpreadsheetApp.openById(Id.of.sheet.I.am.copying).copy(name of sheet);

//Grabbing the folder I want to save to

var specificFolder = Drive.App.getFolderById(folderID.in.quotes);

//saving the copy to that folder (This is the part that I think is wrong but I'm not sure how to fix)

specificFolder.addFile(copyOfSheet);

The goal is that the file would save in this specific folder instead of my general drive. Instead I am getting a not a function error.

Upvotes: 2

Views: 1212

Answers (1)

Tanaike
Tanaike

Reputation: 201613

  • You want to copy a Spreadsheet and move it to the specific folder using Google Apps Script.

If my understanding is correct, how about this answer?

Pattern 1:

In this pattern, your script was modified.

Modified script:

//Copying the sheet (triggered by a form submission)
var copyOfSheet = SpreadsheetApp.openById(Id.of.sheet.I.am.copying).copy(name of sheet).getId();
//Grabbing the folder I want to save to
var specificFolder = DriveApp.getFolderById(folderID.in.quotes);

//saving the copy to that folder
// Retrieve the copied file.
var file = DriveApp.getFileById(copyOfSheet);
// Add the file to "specificFolder".
specificFolder.addFile(file);
// Remove the parent folder of copied file.
file.getParents().next().removeFile(file);
  • At Google Drive, each file can have multiple parent folders. So in your case, new parent folder is added to the copied folder and a parent folder (in this case, it's root folder) is removed from the copied file.

Pattern 2:

In this pattern, the Spreadsheet is directly copied to the specific folder using makeCopy().

Script:

var specificFolder = DriveApp.getFolderById(folderID.in.quotes);
DriveApp.getFileById(Id.of.sheet.I.am.copying).makeCopy(name of sheet, specificFolder);

Note:

  • These patterns supposes that the values of Id.of.sheet.I.am.copying, name of sheet and folderID.in.quotes are correct.

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 1

Related Questions