Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Optimization 将多个电子表格合并到报表文件超过了最大执行时间_Optimization_Google Apps Script_Google Sheets - Fatal编程技术网

Optimization 将多个电子表格合并到报表文件超过了最大执行时间

Optimization 将多个电子表格合并到报表文件超过了最大执行时间,optimization,google-apps-script,google-sheets,Optimization,Google Apps Script,Google Sheets,如果积分小于x,我将使用以下脚本在Google电子表格中添加来自学生循环的文件行。脚本运行良好,但由于电子表格中的数据每天都在添加,现在脚本抛出“超出最大执行时间”错误(我们有2000多个文件)。由于我不熟悉脚本,我不知道如何优化代码 是否有人可以帮助我优化代码或任何解决方案,使执行时间不超过5分钟。每次与电子邮件进行比较时,都必须与许多电子邮件进行比较。请帮忙 function updated() { //Final file data (Combined) var filecom

如果积分小于x,我将使用以下脚本在Google电子表格中添加来自学生循环的文件行。脚本运行良好,但由于电子表格中的数据每天都在添加,现在脚本抛出“超出最大执行时间”错误(我们有2000多个文件)。由于我不熟悉脚本,我不知道如何优化代码

是否有人可以帮助我优化代码或任何解决方案,使执行时间不超过5分钟。每次与电子邮件进行比较时,都必须与许多电子邮件进行比较。请帮忙

function updated() {  
  //Final file data (Combined)
  var filecombined = SpreadsheetApp.openById("XXXXXXXXXX");
  var sheet2 = filecombined.getSheets();

  //Folder with all the files 
  var parentFolder = DriveApp.getFolderById("YYYYYYYYYYYY");
  var files = parentFolder.getFiles();

  //Current Date
  var fecha = new Date();

  //Path for each file in the folder
  while (files.hasNext()) {
    var idarchivo = files.next().getId();
    var sps = SpreadsheetApp.openById(idarchivo);

    var sheet = sps.getSheetByName('STUDENT PROFILE');
    var data = sheet.getDataRange().getValues();
    var credits = data[5][1];

    //Flat; bandera:1 (new row), bandera:2 (update row)
    var bandera = 1;

    //Take data from final file (Combined) 
    var data2 = sheet2[0].getDataRange().getValues();

    //If credits are less than X: write
    if (credits < 120) {
      var email = data[2][1];
      var lastrow = filecombined.getLastRow();
      var u = 0;
      //comparison loop by email, if found it, update and exit the loop
      while (u < lastrow) {
        u = u + 1;
        if (email == data2[u - 1][1]) {
          sheet2[0].getRange(u, 3).setValue(credits);
          sheet2[0].getRange(u, 4).setValue(fecha);
          u = lastrow;
          bandera = 2;
        }
      }
      //if that email does not exist, write a new row
      if (bandera == 1) {
        var nombre = data[0][1];
        sheet2[0].getRange(lastrow + 1, 1).setValue(nombre);
        sheet2[0].getRange(lastrow + 1, 2).setValue(email);
        sheet2[0].getRange(lastrow + 1, 3).setValue(credits);
        sheet2[0].getRange(lastrow + 1, 4).setValue(fecha);
      }
    }
  }
  SpreadsheetApp.flush();
}
函数已更新(){
//最终文件数据(组合)
var filecombined=SpreadsheetApp.openById(“xxxxxxxxx”);
var sheet2=filecombined.getSheets();
//包含所有文件的文件夹
var parentFolder=DriveApp.getFolderById(“yyyyyyyyy”);
var files=parentFolder.getFiles();
//当前日期
var fecha=新日期();
//文件夹中每个文件的路径
while(files.hasNext()){
var idarchivo=files.next().getId();
var sps=电子表格应用程序openById(idarchivo);
var sheet=sps.getSheetByName(“学生档案”);
var data=sheet.getDataRange().getValues();
var信用=数据[5][1];
//展开;bandera:1(新行),bandera:2(更新行)
var bandera=1;
//从最终文件中获取数据(组合)
var data2=sheet2[0].getDataRange().getValues();
//如果积分小于X:写入
如果(学分<120){
var电子邮件=数据[2][1];
var lastrow=filecombined.getLastRow();
var u=0;
//通过电子邮件进行比较循环,如果找到,则更新并退出循环
while(u
提问者的代码运行时间超过4-6分钟,并且出现错误
超过了最长执行时间


以下答案完全基于提问者提供的代码。我们没有关于“filecombined”电子表格及其大小和触发器的任何信息。我们也对各种各样的学生电子表格、大小等一无所知,只是我们知道有2000个这样的文件。我们不知道该例程的运行频率,也不知道有多少学生有学分提问者的代码运行时间超过了4-6分钟,并且出现错误
超过了最大执行时间

以下答案完全基于提问者提供的代码。我们没有关于“filecombined”电子表格及其大小和触发器的任何信息。我们也对各种各样的学生电子表格、大小等一无所知,只是我们知道有2000个这样的文件。我们不知道这个例程多久运行一次,也不知道有多少学生有学分,正如我在中提到的,最大的问题是,当您可以使用查找函数时,您会重复搜索数组中的值

// Create an object that maps an email address to the (last) array
// index of that email in the `data2` array.
const knownEmails = data2.reduce(function (acc, row, index) {
  var email = row[1]; // email is the 2nd element of the inner array (Column B on a spreadsheet)
  acc[email] = index;
  return acc;
}, {});
然后,您可以通过尝试获取电子邮件的值来确定该电子邮件是否存在于
data2
中:

// Get this email's index in `data2`:
var index = knownEmails[email];
if (index === undefined) {
  // This is a new email we didn't know about before
  ...
} else {
  // This is an email we knew about already.
  var u = ++index; // Convert the array index into a worksheet row (assumes `data2` is from a range that started at Row 1)
  ...
}
要了解我们如何从
data2
构建
KnowneMail
,您可能会发现上的文档很有帮助。

正如我在中提到的,您遇到的最大问题是在可以使用查找函数的情况下,重复搜索数组中的值

// Create an object that maps an email address to the (last) array
// index of that email in the `data2` array.
const knownEmails = data2.reduce(function (acc, row, index) {
  var email = row[1]; // email is the 2nd element of the inner array (Column B on a spreadsheet)
  acc[email] = index;
  return acc;
}, {});
然后,您可以通过尝试获取电子邮件的值来确定该电子邮件是否存在于
data2
中:

// Get this email's index in `data2`:
var index = knownEmails[email];
if (index === undefined) {
  // This is a new email we didn't know about before
  ...
} else {
  // This is an email we knew about already.
  var u = ++index; // Convert the array index into a worksheet row (assumes `data2` is from a range that started at Row 1)
  ...
}

要了解我们如何从
data2
构建
KnowneMail
,您可能会发现上的文档很有帮助。

如果您使用
SpreadsheetApp.getActive().getSheetByName(“学生档案”).hideSheet()隐藏工作表,它运行速度会更快吗?您是否正在显示所有2000个文件?也许你也可以把它们藏起来。这样屏幕就不必继续更新,最终结果也是一样的。不过,对此并不确定。可能是重复的,而不是在
while(u
循环中调用
setValue
,将这些数据存储到二维数组中,并在循环外部调用
setValues
。您需要读取一次电子邮件,并创建一个支持比数组遍历更快的查找的数据结构。例如,回顾我的解决方案,并。同时阅读此问题:如果使用
SpreadsheetApp.getActive().getSheetByName(“学生档案”).hideSheet()隐藏工作表,有没有可能运行得更快?您是否正在显示所有2000个文件?也许你也可以把它们藏起来。这样屏幕就不必继续更新,最终结果也是一样的。不过,对此并不确定。可能是重复的,而不是在
while(u
循环中调用
setValue
,将这些数据存储到二维数组中,并在循环外部调用
setValues
。您需要读取一次电子邮件,并创建一个支持比数组遍历更快的查找的数据结构。例如,回顾我的解决方案,并。另请阅读此问题: