如何让我的脚本将副本保存到特定文件夹?

时间:2018-01-06 00:27:01

标签: google-apps-script

我使用以下脚本制作我的Google工作表的副本(仅限值和格式)。但是,此脚本将新文件放在我的主谷歌驱动器中,我希望将文件保存到存档文件夹。如何编辑我的脚本来执行此操作?

function copySheetValuesV4(){
    var sourceSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var sourceSheets = sourceSpreadsheet.getSheets();
    var destination = SpreadsheetApp.create('03_'+sourceSpreadsheet.getName()+' _December 2017');
    for (var i = 0; i < sourceSheets.length; i++){
        var sourceSheet = sourceSheets[i];
        if (!sourceSheet.isSheetHidden()) {
            var sourceSheetName = sourceSheet.getSheetName();
            var sValues = sourceSheet.getDataRange().getValues();
            sourceSheet.copyTo(destination)
            var destinationSheet = destination.getSheetByName('Copy of '+sourceSheetName).setName(sourceSheetName);

            destinationSheet.getRange(1,1,sValues.length,sValues[0].length).setValues(sValues);// overwrite all formulas that the copyTo preserved */

        }
        destination.getSheetByName("sheet1").hideSheet() // Remove the default "sheet1" */
    }
}

1 个答案:

答案 0 :(得分:0)

使用DriveApp.getFolderById(folder_id).addFile(DriveApp.getFileById(destination.getId()))。这将获取电子表格ID,然后将电子表格添加到文件夹中。

相关问题