增强学生信息应用程序的性能

时间:2019-02-22 04:41:53

标签: performance google-apps-script query-optimization

我已经使用Google Sheets.构建了一个简单的学生信息平台,该平台允许用户在用户界面上查询,更新和创建新的学生信息。请参阅this sheet以了解其工作原理。

Refresh/Update/Save的功能位于菜单栏上的“操作”按钮内。一切似乎都运行良好,但是当记录数量增加时(例如超过100条记录),所有功能都会变慢,而超过200条记录会变得非常慢。

感谢有人怀疑我是否需要对其进行优化,以帮助他们了解这些脚本。

非常感谢!

function UpdateDataIntoMaster() { //This script is used in the SAVE button in UPDATE sheet)

/*Get data from UPDATE Sheet*/
  var ss = SpreadsheetApp.openById("11Djp9UmXbtWv7VitZFfo0X4Ctet3O8Amh4xADNKOZgY");
  var sheet = ss.getSheetByName('UPDATE');
  var range = sheet.getRange("D30:AE30"); //All data transposed into this line. MUST be updated if more fields are added into the Data sheet
  var values = range.getValues();
  var rangeForKey =  sheet.getRange("D30") //Student Name is used as the 
key identifier
  var keyValue = rangeForKey.getValue(); 

/*Pass in keyValue(identifier = Student Name)
  and all data in the function below in order 
  to update master data sheet*/
  updDbase(keyValue,values);

function updDbase(keyValue,values) {
  var ss = SpreadsheetApp.openById("11Djp9UmXbtWv7VitZFfo0X4Ctet3O8Amh4xADNKOZgY")
  var sheet = ss.getSheetByName('Data');
  var data = sheet.getDataRange().getValues();
  var noOfRow = values.length
  var noOfCol = values[0].length

    for (var i=0; i < data.length; i++) { // going through all the rows in Data sheet
        var keyData = ss.getSheetByName("Data").getRange(i+1,1).getValue(); //Get the Student Name from Data sheet
        if (keyData == keyValue) {      
     //   for (var j=0; j < data[i].length; j++) { // this is going through all the cell of a row
           var row = Number(i)+1;

           var sh = SpreadsheetApp.getUi();
           var response = sh.alert("Update Information","Are you sure you want to update the student information?", sh.ButtonSet.YES_NO);
           if (response == sh.Button.YES)
            {
              var sheets = ss.getSheetByName("Data").getRange(row,1,noOfRow,noOfCol).setValues(values);      
            }//If  response == YES

   }           
  }        
 }  
}

function CreateNew() {

/*Get data from Inquiry Sheet*/
  var ss = SpreadsheetApp.openById("11Djp9UmXbtWv7VitZFfo0X4Ctet3O8Amh4xADNKOZgY");
  var sheetNew = ss.getSheetByName('Create New');
  var range = sheetNew.getRange("D30:AZE30"); //All data transposed into 
this line
  var values = range.getValues();
  var rangeForKey =  sheetNew.getRange("E30") //Using Student ID as key identifier
  var keyValue = rangeForKey.getValue(); 
  var noOfRow = values.length
  var noOfCol = values[0].length
  var sheetData = ss.getSheetByName('Data');
  var lastRow = sheetData.getLastRow();
  var data = sheetData.getDataRange().getValues();

    for (var i=0; i < data.length; i++) { // going through all the rows in Data sheet
        var keyData = sheetData.getRange(i+1,2).getValue(); //Get the Student ID from Data sheet
        if (keyData == keyValue) {      
           AlertBox();//If Student ID is found, to prompt Student ID already 
exist
           return; 
         } //If
     }    //For
/*Confirming with user whether to proceed to create new entry*/       
       var sh = SpreadsheetApp.getUi();
       var response = sh.alert("Create New Record","Are you sure you want to 
create new student information?", sh.ButtonSet.YES_NO);
       if (response == sh.Button.YES){
         if (keyValue == ""){ 
             var response = sh.alert("Create New Record","Unable to proceed 
because Student ID is empty", sh.ButtonSet.OK);
             return;}
          else {
              //var response = sh.alert("Create New Record","Unable to 
proceed because Student ID is empty", sh.ButtonSet.OK);
              var sheets = 
sheetData.getRange(lastRow+1,1,1,noOfCol).setValues(values) 
               }
         }//If
}  

function EditStudentInfo() {
 var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Inquiry");  
 //var ss = SpreadsheetApp.getActive();
  var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  for (var i = 0; i < protections.length; i++) {
    var protection = protections[i];
     if (protection.canEdit()) {
       protection.remove();
     }
}
}

function EditContent() {

     var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Update");

     var rangeContentCol1 = s.getRange("E3:E23");
     var CopyContentCol1  = s.getRange("E3:E23").getValues();
     var rangeContentCol2 = s.getRange("I3:I23");
     var CopyContentCol2  = s.getRange("I3:I23").getValues();

     rangeContentCol1.clearContent();
     rangeContentCol2.clearContent();

     var PasteContentCol1  = 
s.getRange("E3:E23").setValues(CopyContentCol1); 
     var PasteContentCol2  = s.getRange("I3:I23").setValues(CopyContentCol2); 
}

1 个答案:

答案 0 :(得分:1)

使用Apps Script的人员经常遇到性能错误,即在其for循环中执行cordova plugin remove cordova-plugin-camera && cordova plugin add https://github.com/apache/cordova-plugin-camera 。在性能方面,这些get和set调用非常昂贵。

幸运的是,此修复很容易-一次获取所有数据,然后循环遍历。 您实际上已经这样做了。在脚本中,您可以获得整个数据范围,但随后仅使用部分数据,而是执行另一个getValues调用。我已经更新了脚本中在for循环中有.getRange().getValues()个调用的两个区域-> getRange()

var keyData = data[i][0];

对此进行测试,让我知道是否有帮助!