简体   繁体   中英

Automatic Google Sheets Back Up without Copying the Forms attached to the Sheets

How do I make a script that copies a Google Sheet as a backup into a backup folder in the Google Drive, but not making copies of the Google Forms that are attached to the Sheet?

function backUp() {
    var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
    var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Copy " + formattedDate;
    var destination = DriveApp.getFolderById("1NNhZESRq_0_DVBwzA8fvxSYGMpOa6KC_");
    var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())

    file.makeCopy(name, destination);
}

This one automatically copies the original files into the Backup Folder, but it also copies the Google Forms that are linked to it... so I end up with 10+ copies every day after it saves.

Great question. It was little hard for my knowledge, I catch it :)

Is unable to copy whole Spreadsheet without Form, because they are magically connected (magically ==> connection is under any public API possibilities).

You can take to use the copy sheet to another spreadsheed feature.

I tried it and it doesn't create copy of Form, yeah!

But for this case have Script & Drive unwieldy API. Result looks too hacky:

function backUp() {
    //Open old spreadsheet
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

    //Prepare new name
    var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
    var name = spreadsheet.getName() + " Copy " + formattedDate;

    //Prepare file system objects
    var oldfile = DriveApp.getFileById(spreadsheet.getId());
    var destination = DriveApp.getFolderById("1NNhZESRq_0_DVBwzA8fvxSYGMpOa6KC_");

    //create new spreadsheet
    var newSpreadsheet = SpreadsheetApp.create(name);

    //move to destination folder (yep, it's hacky)
    var newFile = DriveApp.getFileById(newSpreadsheet.getId());
    var parents = newFile.getParents();
    while (parents.hasNext()) {
        var parent = parents.next();
        parent.removeFile(newFile); //remove from default folder
    }
    destination.addFile(newFile);

    //copy all sheets to new spreadsheet
    var sheets = spreadsheet.getSheets();
    sheets.forEach(function(sheet) {
        sheet.copyTo(newSpreadsheet);
    });

    //remove empty sheet (created with new spreadsheet)
    newSpreadsheet.deleteSheet(newSpreadsheet.getSheets()[0]);
}

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