谷歌App脚本一个功能需要10分钟,但复制时超过时间限制。

问题描述 投票:0回答:1

第一次发帖,所以我很抱歉,如果我没有遵循正确的礼仪。

所以我有代码,本质上,在指定的文件夹中创建70个预测文档。这工作得很好。

我使用了同样的代码,重新指定了所有的文件夹引用,但现在超时了。

代码如下,有什么建议可以让它运行得更快?

function createFiles(weeknr, masterIDs, masterfolders, mastercodes, archivefolders) {
    var ss = SpreadsheetApp.getActiveSpreadsheet()
    var date = new Date()
    var textdate = Utilities.formatDate(date, "Europe/London", "dd/MM/YYYY HH:mm")
    var fileslog = ss.getSheetByName("FileList")
    var year = date.getYear()

    var mainfolder = DriveApp.getFolderById("1HdG3eJZrIMF-CRpx17Reqdyd7JM5v5ZW")
    var weekfolder = mainfolder.createFolder('Week ' + weeknr + ' ' + year)


    var tempArray = []
    for (var i = 0; i < masterIDs.length; i++) {

        //   for (var i=0;i<masterIDs.length;i++){
        var folderID = masterfolders[i]
        var masterID = masterIDs[i]
        var code = mastercodes[i]
        var archivefolder = archivefolders[i]

        var newfile = DriveApp.getFileById(masterID).makeCopy(code + ' DCS WK' + weeknr,
            DriveApp.getFolderById(folderID))
        var newID = newfile.getId()

        tempArray.push([newfile.getName(), newID, newfile.getUrl(),
        newfile.getDateCreated(), "Created", folderID, archivefolder, weeknr])

        SpreadsheetApp.openById(newID).getSheetByName("Main").getRange(3, 3).setValue(weeknr)
        weekfolder.addFile(newfile)

        //NEW UPDATE 14/02 to set the owner to network finance
        newfile.setOwner('[email protected]')
        //End of UPDATE


        fileslog.getRange(fileslog.getLastRow() + 1, 1, 1, 8).setValues([tempArray[i]])
    }
    // Logger.log(tempArray )
    // if(tempArray.length>0){
    //     fileslog.getRange(fileslog.getLastRow()+1, 1, tempArray.length, 
    8).setValues(tempArray)
    // }
}


function archiveFiles(weeknr, fileIDs, masterfolders, mastercodes, archivefolders) {
    var ss = SpreadsheetApp.getActiveSpreadsheet()
    var date = new Date()
    var textdate = Utilities.formatDate(date, "Europe/London", "dd/MM/YYYY HH:mm")
    var fileslog = ss.getSheetByName("FileList")
    //var year = date.getYear()

    //var mainfolder = DriveApp.getFolderById("1HdG3eJZrIMF-CRpx17Reqdyd7JM5v5ZW")
    //var weekfolder = mainfolder.createFolder('Week '+weeknr+' '+year)

    var tempArray = []

    for (var i = 0; i < fileIDs.length; i++) {
        //   for (var i=0;i<masterIDs.length;i++){
        var folderID = masterfolders[i]
        var fileID = DriveApp.getFileById(fileIDs[i])
        var code = mastercodes[i]
        var archivefolder = archivefolders[i]

        DriveApp.getFolderById(archivefolder).addFile(fileID)
        DriveApp.getFolderById(folderID).removeFile(fileID)
        //var newfile = DriveApp.getFileById(masterID).makeCopy(code + ' DCS WK'+weeknr, 
        DriveApp.getFolderById(folderID))
        //var newID = newfile.getId()





        tempArray.push([fileID.getName(), fileIDs[i], fileID.getUrl(),
        fileID.getLastUpdated(), "Archived", masterfolders[i], archivefolders[i], weeknr])

        //weekfolder.addFile(newfile)

    }
    Logger.log(tempArray)
    if (tempArray.length > 0) {
        fileslog.getRange(fileslog.getLastRow() + 1, 1, tempArray.length, 8).setValues(tempArray)
    }
}
javascript google-apps-script optimization google-sheets google-drive-api
1个回答
0
投票

发布的答案是为了文档的目的。

正如评论中讨论的那样,问题是文件大小,把复制的文件分批拆开就可以了。

结果发现,文件实在是太大了,无法在规定的时间内创建70个文件。所以我把它分割成30个批次,它的工作完美无缺。

© www.soinside.com 2019 - 2024. All rights reserved.