Google Spreadsheet, script, backup file, subfolder

2019-07-19 13:42发布

I need to backup a file on a daily basis, I have resolved this issue using the following script:

  function myFunction() {
      DocsList.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).makeCopy(SpreadsheetApp.getActiveSpreadsheet().getName() + "_Backup");
    }

And I'm using the Time-driven trigger to set the hour I want the script creates the backup file. However, I would like these daily backups going to a specific Subfolder, lets call it "Daily Backup Folder".

Can someone help me with a script for that?

Thanks in advance!

2条回答
手持菜刀,她持情操
2楼-- · 2019-07-19 13:55

makeCopy(name) is what you are using.

makeCopy(name, destination) is what you need to use.

Solution:

You will need to get your folder's ID. Open your the folder where you want to save your backup files in google drive and check it's URL in the browser's address bar.

It will look like:"https ://drive.google.com/drive/folders/zzzzz". Copy this zzzzz and replace the xxxxxx in the code below.

function myFunction() {
      DocsList.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).makeCopy(SpreadsheetApp.getActiveSpreadsheet().getName() + "_Backup", DriveApp.getFolderById("xxxxxxxxxx"));
    }
查看更多
Fickle 薄情
3楼-- · 2019-07-19 14:11

using DocsList service: try:

function backUp() {
var backup = DocsList.getFileById(SpreadsheetApp.getActiveSpreadsheet()
    .getId())
    .makeCopy(SpreadsheetApp.getActiveSpreadsheet()
        .getName() + "_Backup");
backup.addToFolder(DocsList.getFolder('TEST BACKUP'));
backup.removeFromFolder(DocsList.getRootFolder());
}

However, since DocsList service is depreciated, you may want to consider Drive service. Try:

function backUP() {
DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet()
    .getId())
    .makeCopy(SpreadsheetApp.getActiveSpreadsheet()
        .getName() + "_Backup", (DriveApp.getFolderById('folder_id')));
}

Fill in the actual id of the folder 'Daily Backup folder' in the last line of the script and see if that works ?

查看更多
登录 后发表回答