Google Spreadsheet, script, backup file, subfolder

好久不见. 提交于 2019-12-24 03:19:21

问题


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!


回答1:


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 ?




回答2:


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"));
    }


来源:https://stackoverflow.com/questions/29179672/google-spreadsheet-script-backup-file-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!