Search code examples
google-apps-scriptgoogle-sheetscustom-function

Execute formula next 10 rows


I have a concat_custom script I'd like to execute 10 rows at a time.

For example, in this sheet:

enter image description here

https://docs.google.com/spreadsheets/d/1O1rZUstDNSXPdUVXvaDfPO4rAQs2cJWHimfGxbddtNU/edit#gid=450240458

Custom function:

function concat_custom(s, array) {
    array = conformToOneDimensionalArray(array);

    return array.filter(function (value) {
      return value != "";
    }).join("-");
}

function conformToOneDimensionalArray(value) {
  if (!Array.isArray(value)) {
    value = [value];
  }
  else if (Array.isArray(value[0])) {
    value = value.reduce(function(a, b) {
      return a.concat(b);
    });
  }

  return value;
}

I'd like column C to have text Ted-John and not =concat_custom("-",A1:B1) so column C will be independent of A & B. How can I process say 10 rows at a time in this example?

I'd like to be able to process 10 rows every minute through timed triggers. So it checks the last row then continues from there.


Solution

  • Try this:

    function withadash() {
      var ss=SpreadsheetApp.getActive();
      var sh=ss.getActiveSheet();
      var nextrow=getNextRowInColumn3();
      if(nextrow>0) {    
        var rg=sh.getRange(nextrow,1,10,3);
        var vA=rg.getValues()
        for(var i=0;i<vA.length;i++) {
          vA[i][2]=vA[i][0]+'-'+vA[i][1];
        }
        rg.setValues(vA);
      }
    }
    
    function getNextRowInColumn3() {
      var ss=SpreadsheetApp.getActive();
      var sh=ss.getActiveSheet();
      var rg=sh.getRange(1,3,sh.getLastRow(),1);
      var vA=rg.getValues();
      for(var i=0;i<vA.length;i++) {
        if(!vA[i][0]) {
          return i+1;
        }
      }
      return 0;
    }