How to loop in .gs for record details

-1

I have a custom form which requires you to build an N quantity of inputs and store them in a spreadsheet, each data must be registered as a list according to the number of inputs created. It is possible to do it from the main so that it registers only the amount of created inputs (I have a limit of 10 but I do not want it to store null records)

function processRef(e){ 
  for (var i=1; i<11; i++){
   var sNroref[i] = e.nroref[i];
  }

  var hojaDatos = spreadSheet.getSheetByName('Refs'); 
  var ultimaFila = hojaDatos.getLastRow();

  for (var i=1; i<11; i++){
   hojaDatos.getRange(ultimaFila+1,1).setValue(sNroref[i]);
   SpreadsheetApp.flush();
 }
}

So far I've only made this work:

function processReferenciaDetalle(e){
  var sMtempo = new Date();
  var sMTpRec = e.MTpRec;
  var sFecent = e.fecent;
  var sNroref1 = e.nroref1;
  var sNroref2 = e.nroref2;
  var sNroref3 = e.nroref3;
  var sNroref4 = e.nroref4;
  var sNroref5 = e.nroref5;
  var sDirent = e.dirent;

  var hojaDatos = spreadSheet.getSheetByName('Referencias'); 
  var ultimaFila = hojaDatos.getLastRow();

  hojaDatos.getRange(ultimaFila+1,1).setValue(sMtempo);
  hojaDatos.getRange(ultimaFila+1,2).setValue(sMTpRec);
  hojaDatos.getRange(ultimaFila+1,3).setValue(sFecent);
  hojaDatos.getRange(ultimaFila+1,4).setValue(sNroref1);
  hojaDatos.getRange(ultimaFila+1,5).setValue(sDirent);
  if (sNroref2!=""){
   hojaDatos.getRange(ultimaFila+2,1).setValue(sMtempo);
   hojaDatos.getRange(ultimaFila+2,2).setValue(sMTpRec);
   hojaDatos.getRange(ultimaFila+2,3).setValue(sFecent);
   hojaDatos.getRange(ultimaFila+2,4).setValue(sNroref2);
   hojaDatos.getRange(ultimaFila+2,5).setValue(sDirent);
  }
  ...
  if (sNroref5!=""){
   hojaDatos.getRange(ultimaFila+5,1).setValue(sMtempo);
   hojaDatos.getRange(ultimaFila+5,2).setValue(sMTpRec);
   hojaDatos.getRange(ultimaFila+5,3).setValue(sFecent);
   hojaDatos.getRange(ultimaFila+5,4).setValue(sNroref5);
   hojaDatos.getRange(ultimaFila+5,5).setValue(sDirent);
  }
  SpreadsheetApp.flush();
}

With a loop I would have a cleaner code but I can not get it to work.

    
asked by Karen Lewis 24.02.2018 в 00:01
source

2 answers

0

The problem is not the loop itself, but as it refers to the data received from the form, until now the most feasible solution has been to store the references in an array, which allows a cleaner and shorter code.

function processReferenciaDetalle(e) {
  var sMTpRec = e.MTpRec;
  var sFecent = e.fecent;
  var sDirent = e.dirent;

  var hojaDatos = spreadSheet.getSheetByName('Referencias'); 
  var ultimaFila = hojaDatos.getLastRow();

  var navInput = [e.nroref1, e.nroref2, e.nroref3, e.nroref4, e.nroref5];
  for(var i = 0; i < e.nrosac; i++){ //Cantidad de registros esperada
   var sNroref = navInput[i];
   var ind = i+1;
   if (sNroref != ""){
    hojaDatos.getRange(ultimaFila+ind,1).setValue(sMtempo);
    hojaDatos.getRange(ultimaFila+ind,2).setValue(sMTpRec);
    hojaDatos.getRange(ultimaFila+ind,3).setValue(sFecent);
    hojaDatos.getRange(ultimaFila+ind,4).setValue(sNroref);
    hojaDatos.getRange(ultimaFila+ind,5).setValue(sDirent);
   }    
  }
  SpreadsheetApp.flush();
 }
    
answered by 17.03.2018 в 04:50
-1
  

Is it possible to loop the main Google Apps Script?

Understanding "main" a server-side code file (.gs extension), since in Google Apps Script it is possible to use most ECMAScript 5 statements for what you can use, all of which are mentioned in Loops and iteration from Mozilla Developer Network.

  

The statements for loops available in JavaScript are:

     
  • sentence for
  •   
  • sentence do ... while
  •   
  • while statement
  •   
  • label statement
  •   
  • break statement
  •   
  • continue sentence
  •   
  • sentence for ... in
  •   
  • sentence for ... of
  •   

Personally I have not tried label.

Example

The following example prints a text in the Google Apps Script Project Editor records

function ejemploFor() {
  for(var i = 0; i < 5; i++){
    Logger.log('Iteración número ' + (i + 1));
  }
}

Result:

[18-02-24 19:31:12:060 CST] Iteración número 1
[18-02-24 19:31:12:061 CST] Iteración número 2
[18-02-24 19:31:12:061 CST] Iteración número 3
[18-02-24 19:31:12:062 CST] Iteración número 4
[18-02-24 19:31:12:063 CST] Iteración número 5
    
answered by 24.02.2018 в 15:12