简体   繁体   中英

Script to save full Google Spreadsheet to folder

I want to create a function to save a spreadsheet to a folder. I'm almost there, I think I just need to adjust the synthax of the createFile class.

Here is my code:

const spreadsheet2 = SpreadsheetApp.getActiveSpreadsheet();
var folder2 = DriveApp.getFoldersById("my_folder_id").next();
var filename2 = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("B97").getValue();

folder2.createFile([I think here is my missing command].setName(filename2));

Could someone finish it?

  • You want to copy the active Spreadsheet to the specific folder.
  • You want to use the filename from getActiveSheet().getRange("B97").getValue() .
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification points:

  • At DriveApp.getFoldersById("my_folder_id").next() , there is no method of getFoldersById . When you want to use the folder ID, please use DriveApp.getFolderById(folderId) .
  • Unfortunately, the Spreadsheet cannot be directly created with createFile() . In your case, in order to copy the active Spreadsheet, you can use makeCopy() .

When above points are reflected to your script, it becomes as follows.

Modified script:

var folderId = "###";  // Please set the folder ID you want to put the copied Spreadsheet.

const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var filename2 = spreadsheet.getActiveSheet().getRange("B97").getValue();

var folder = DriveApp.getFolderById(folderId);
DriveApp.getFileById(spreadsheet.getId()).makeCopy(filename2, folder);

References:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM