错误值错误Google Apps脚本

时间:2017-09-14 17:07:44

标签: javascript google-apps-script

我每次运行代码后都会收到一条错误,指出“第35行的值不正确”。代码实际上一直运行一次,但是然后有一个名为“undefined”的第二个文件,似乎是这个错误的一部分。我的代码中没有任何地方可以创建多个电子表格,我们只创建一个电子表格。如果我能找到解决这个问题的方法,我将不胜感激。代码如下:

function ssCreation(spreadsheetName, spreadsheetId1, sourceSheetName1,
    spreadsheetId2, sourceSheetName2,
    spreadsheetId3, sourceSheetName3,
    spreadsheetId4, sourceSheetName4,
    spreadsheetId5, sourceSheetName5,
    spreadsheetId6, sourceSheetName6,
    spreadsheetId7, sourceSheetName7,
    spreadsheetId8, sourceSheetName8,
    spreadsheetId9, sourceSheetName9,
    spreadsheetId10, sourceSheetName10,
    spreadsheetId11, sourceSheetName11) {

    var create = SpreadsheetApp.create(spreadsheetName).getId();
    var renameSheet = SpreadsheetApp.openById(create).renameActiveSheet('1');
    for (var i = 1; i < 14; i++) {
        var sheetName = 1 + i;
        var insertTheSheet = SpreadsheetApp.openById(create).insertSheet(i);
    }

    var ss1 = SpreadsheetApp.openById(spreadsheetId1);
    var ssd1 = SpreadsheetApp.openById(create);
    var sourceSheet1 = ss1.getSheetByName(sourceSheetName1);
    var sourceData1 = sourceSheet1.getDataRange().getValues();
    var destSheet1 = ssd1.getSheetByName("1");
    destSheet1.getRange(destSheet1.getLastRow() + 1, 1, sourceData1.length, sourceData1[0].length).setValues(sourceData1);

    var ss2 = SpreadsheetApp.openById(spreadsheetId2);
    var ssd2 = SpreadsheetApp.openById(create);
    var sourceSheet2 = ss2.getSheetByName(sourceSheetName2);
    var sourceData2 = sourceSheet2.getDataRange().getValues();
    var destSheet2 = ssd2.getSheetByName("Sheet2");
    destSheet2.getRange(destSheet2.getLastRow() + 1, 1, sourceData2.length, sourceData2[0].length).setValues(sourceData2);

    var ss3 = SpreadsheetApp.openById(spreadsheetId3);
    var ssd3 = SpreadsheetApp.openById(create);
    var sourceSheet3 = ss3.getSheetByName(sourceSheetName3);
    var sourceData3 = sourceSheet3.getDataRange().getValues();
    var destSheet3 = ssd3.getSheetByName("Sheet3");
    destSheet3.getRange(destSheet3.getLastRow() + 1, 1, sourceData3.length, sourceData3[0].length).setValues(sourceData3);

    var ss4 = SpreadsheetApp.openById(spreadsheetId4);
    var ssd4 = SpreadsheetApp.openById(create);
    var sourceSheet4 = ss4.getSheetByName(sourceSheetName4);
    var sourceData4 = sourceSheet4.getDataRange().getValues();
    var destSheet4 = ssd4.getSheetByName("Sheet4");
    destSheet4.getRange(destSheet4.getLastRow() + 1, 1, sourceData4.length, sourceData4[0].length).setValues(sourceData4);

    var ss5 = SpreadsheetApp.openById(spreadsheetId5);
    var ssd5 = SpreadsheetApp.openById(create);
    var sourceSheet5 = ss5.getSheetByName(sourceSheetName5);
    var sourceData5 = sourceSheet5.getDataRange().getValues();
    var destSheet5 = ssd5.getSheetByName("Sheet5");
    destSheet5.getRange(destSheet5.getLastRow() + 1, 1, sourceData5.length, sourceData5[0].length).setValues(sourceData5);

    var ss6 = SpreadsheetApp.openById(spreadsheetId6);
    var ssd6 = SpreadsheetApp.openById(create);
    var sourceSheet6 = ss6.getSheetByName(sourceSheetName6);
    var sourceData6 = sourceSheet6.getDataRange().getValues();
    var destSheet6 = ssd6.getSheetByName("Sheet6");
    destSheet6.getRange(destSheet6.getLastRow() + 1, 1, sourceData6.length, sourceData6[0].length).setValues(sourceData6);

    var ss7 = SpreadsheetApp.openById(spreadsheetId7);
    var ssd7 = SpreadsheetApp.openById(create);
    var sourceSheet7 = ss7.getSheetByName(sourceSheetName7);
    var sourceData7 = sourceSheet7.getDataRange().getValues();
    var destSheet7 = ssd7.getSheetByName("Sheet7");
    destSheet7.getRange(destSheet7.getLastRow() + 1, 1, sourceData7.length, sourceData7[0].length).setValues(sourceData7);

    var ss8 = SpreadsheetApp.openById(spreadsheetId8);
    var ssd8 = SpreadsheetApp.openById(create);
    var sourceSheet8 = ss8.getSheetByName(sourceSheetName8);
    var sourceData8 = sourceSheet8.getDataRange().getValues();
    var destSheet8 = ssd8.getSheetByName("Sheet8");
    destSheet8.getRange(destSheet8.getLastRow() + 1, 1, sourceData8.length, sourceData8[0].length).setValues(sourceData8);

    var ss9 = SpreadsheetApp.openById(spreadsheetId9);
    var ssd9 = SpreadsheetApp.openById(create);
    var sourceSheet9 = ss9.getSheetByName(sourceSheetName9);
    var sourceData9 = sourceSheet9.getDataRange().getValues();
    var destSheet9 = ssd9.getSheetByName("Sheet9");
    destSheet9.getRange(destSheet9.getLastRow() + 1, 1, sourceData9.length, sourceData9[0].length).setValues(sourceData9);

    var ss10 = SpreadsheetApp.openById(spreadsheetId10);
    var ssd10 = SpreadsheetApp.openById(create);
    var sourceSheet10 = ss10.getSheetByName(sourceSheetName10);
    var sourceData10 = sourceSheet10.getDataRange().getValues();
    var destSheet10 = ssd10.getSheetByName("Sheet10");
    destSheet10.getRange(destSheet10.getLastRow() + 1, 1, sourceData10.length, sourceData10[0].length).setValues(sourceData10);

    var ss11 = SpreadsheetApp.openById(spreadsheetId11);
    var ssd11 = SpreadsheetApp.openById(create);
    var sourceSheet11 = ss11.getSheetByName(sourceSheetName11);
    var sourceData11 = sourceSheet11.getDataRange().getValues();
    var destSheet11 = ssd11.getSheetByName("Sheet11");
    destSheet11.getRange(destSheet11.getLastRow() + 1, 1, sourceData11.length, sourceData11[0].length).setValues(sourceData11);

}

1 个答案:

答案 0 :(得分:1)

不是将23个参数传递给函数来创建新的电子表格,而是从其他11个电子表格中复制数据,为什么不将副本转出另一个函数&amp;然后将源表信息传递给该函数?这样,您只需要为每个复制操作调试代码段,而不是在当前版本中调试11个重复项。下面我总结了一种方法:你重复的6行数据复制代码现在是def update_plants(): for shrub in spr_plant_group: shrub.age += 1 # Respawn a new plant in a different place if shrub.age >= 200: shrub.kill() plant.append (Plant()) 函数中的5行。

我们创建了目标电子表格&amp;当我们将Spreadsheet对象传递给copyData()时,只引用它一次。数组copyData()是一种key =&gt;值对列表。因此,要从源位置复制到目标位置,我们会为sheetsList[]中的每个key =&gt;值对调用copyData()一次。我们将“spreadsheetId N ”(键),“sourceSheetName N ”(值)与sheetsList[]对象&amp;传递给copyData()目标电子表格中将接受来自源的数据的工作表的名称。

希望这会有所帮助。

destinationSpreadsheet