I am using DocsList.find() and everything in my code is working just fine. The only issue is that it can take up to 90 seconds to find a file in the folder specified even when there are only two files in the folder!

I was hoping that someone might be able to suggest a method of speeding up the execution. Or, it may be that I have misunderstood the folder element of DocsList. Any help would be greatly appreciated!

Thanks,

Oli

Sample Code:

function myFunction() {
var folder = DocsList.getFolderById("MYFOLDERID");
var lastrow = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1').getLastRow();
var thissheet = SpreadsheetApp.openById(SpreadsheetApp.getActiveSpreadsheet().getId());

var i = 3;
while (i<= lastrow)
if(SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Add scores to sheets').getRange(i,6).getValue() < 1){

 var getfile = folder.find(thissheet.getSheetByName('Add scores to sheets').getRange(i,3).getValue())[0].getId();
  var cell = SpreadsheetApp.openById(getfile).getSheetByName('Sheet1').getRange(thissheet.getSheetByName('Add scores to sheets').getRange(i,5).getValue());

  if(cell.getValue() < thissheet.getSheetByName('Add scores to sheets').getRange(i,4).getValue()){
  cell.setValue(thissheet.getSheetByName('Add scores to sheets').getRange(i,4).getValue());
  }


  thissheet.getSheetByName('Add scores to sheets').getRange(i,6).setValue(1);
   i++ 
}

else {i++}

}

The code takes a score form the sheet 'Add scores to sheet', finds the file named in column C (an email address), locates the named range and sets the value providing that the current value is less than the new value. I am a Maths teacher - this is all run from a Google form quiz.

有帮助吗?

解决方案

We had the same issue, Folder.find() was taking around 40 seconds to scan a single folder and to return just one file.

A way much faster solution is to use Folder.getFiles() and loop on the resultset to get what you need..

For example, if you need to delete all files that have "DELETED" string in filename:

[SLOW] folder.find()

var files = folder.find("DELETE");
for(var x = 0; x < files.length; x++){
  files[x].removeFromFolder(folder)
}

[FAST] folder.getFiles()

var files = folder.getFiles();
for(var x = 0; x < files .length; x++){
  var fileName = files[x].getName();
  if(fileName.indexOf("DELETE") > - 1){
    files[x].removeFromFolder(folder)
  }
}

The second version, in our particular case scenario, took only 3 seconds to complete.

其他提示

As the above comment says, you may want to start by limiting calls to getSheetByName. If you declare a variable, like so:

var addScoresToSheetsSheet = 
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Add scores to sheets')

And then use that variable name in your while loop, like this:

if(addScoresToSheetsSheet.getRange(i,6).getValue() < 1){

 var getfile = folder.find(addScoresToSheetsSheet.getRange(i,3).getValue())[0].getId();
  var cell = SpreadsheetApp.openById(getfile).getSheetByName('Sheet1').getRange(addScoresToSheetsSheet).getRange(i,5).getValue());

  if(cell.getValue() < addScoresToSheetsSheet.getRange(i,4).getValue()){
  cell.setValue(addScoresToSheetsSheet).getRange(i,4).getValue());
  }


  addScoresToSheetsSheet.getRange(i,6).setValue(1);
   i++ 
}

It not only makes the code more readable, but also will make it execute faster. If this is not sufficient, then you may want to read the entire sheet into an array, and then execute your code on the array, as mentioned here:

https://developers.google.com/apps-script/best_practices#batchOperations

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top