Jonas
Jonas

Reputation: 11

How to loop 2 Dimensional Array on Google Script

There is a problem with my run time execution in my code. It takes too long to finish, since I have to loop a large amounts of data to look for matching data. I used array on the first loop value though, I don't know how to array the second value without affecting the first array.

Name of the first array : Source
Name of the Second array : Target

Here is my Code:

function inactive_sort_cebu() {

  var ss = SpreadsheetApp.openById('169vIeTMLK4zN5VGCw1ktRteCwMToU8eGABFDxg52QBk');
//  var sheet =  ss.getSheets()[0];// Manila Roster
   var sheet2 =  ss.getSheets()[1];// Cebu Roster

   var column = sheet2.getRange("C1:C").getValues();
   var last = column.filter(String).length;


//  -----------------------------------------------------------
   var ss1 = SpreadsheetApp.openById('153ul2x2GpSopfMkCZiXCjmqdPTYhx4QiOdP5SBYzQkc');

//   var sched_sheet =  ss1.getSheets()[0];// ScheduledForm_Manila
   var sched_sheet2 =  ss1.getSheets()[1];// ScheduledForm_Cebu

   var column2 = sched_sheet2.getRange("C1:C").getValues();
   var last2 = column2.filter(String).length;

////  -------------------------Manila-Roster---------------------------------

    var i= 2;

   var column3 = sched_sheet2.getRange("J1:J").getValues();
   var a = column3.filter(String).length - 1;
//  var a = 0; 
  try{

  var source = sched_sheet2.getRange("C2:C").getValues();

  for (a;a<=last2;){
   /// this is the code that i need to array without affecting the other array which is the source variable
    var target = sheet2.getRange("C"+ i).getValue();

    if(source[a] == target){
           //    Get "No Schedule Request data on Cell H
         var data = sched_sheet2.getRange("H"+(a+2)).getValue();
          //    Get "Schedule Request data on Cell F
          var  data1 = sched_sheet2.getRange("F"+(a+2)).getValue();
           var condition_1 = sched_sheet2.getRange("D"+(a+2)).getValue();
           var condition_2 = sched_sheet2.getRange("G"+(a+2)).getValue();
           var format_Con_2 = Utilities.formatDate(condition_2, 'Asia/Manila', 'm/dd/yyyy');
           var condition_3 = sched_sheet2.getRange("K"+ (a+2)).getValue();

           var date = new Date();
           var date_Manila = Utilities.formatDate(date, 'Asia/Manila', 'm/dd/yyyy');
         if(condition_1 == "No Schedule Request" && format_Con_2 <= date_Manila && condition_3 ==""){

           sheet2.getRange("AA"+ i).setValue("N - "+ data);
           sched_sheet2.getRange("J"+ (a+2)).setValue("Cebu");
           sched_sheet2.getRange("K"+ (a+2)).setValue("Done");
           a++;
             }
      else if (condition_1 == "Schedule Request" && format_Con_2 <= date_Manila && condition_3 ==""){

           sheet2.getRange("AA"+ i).setValue("Y - "+data1);
           sched_sheet2.getRange("J"+ (a+2)).setValue("Cebu");
           sched_sheet2.getRange("K"+ (a+2)).setValue("Done");
           a++; 
             }
         else{a++;}

        i=2;}   


   else {i++;}

  }

Upvotes: 1

Views: 143

Answers (1)

Cooper
Cooper

Reputation: 64040

This is a simple example of a web app that puts an editable spreadsheet on an HTML Page. Publish as a webapp. I loops through the 2D array that you get when you getValues from the getDataRange() method. In this case I'm just intertwining html into the mix.

Code.gs:

var SSID='SpreadsheetID';
var sheetName='Sheet Name';
function htmlSpreadsheet(mode)
{
  var mode=(typeof(mode)!='undefined')?mode:'dialog';
  var br='<br />';
  var s='';
  var hdrRows=1;
  var ss=SpreadsheetApp.openById(SSID);
  var sht=ss.getSheetByName(sheetName);
  var rng=sht.getDataRange();
  var rngA=rng.getValues();
  s+='<table>';
  for(var i=0;i<rngA.length;i++)
  {
    s+='<tr>';
    for(var j=0;j<rngA[i].length;j++)
    {
      if(i<hdrRows)
      {
        s+='<th id="cell' + i + j + '">' + '<input id="txt' + i + j + '" type="text" value="' + rngA[i][j] + '" size="10" onChange="updateSS(' + i + ',' + j + ');" />' + '</th>';
      } 
      else
      {
        s+='<td id="cell' + i + j + '">' + '<input id="txt' + i + j + '" type="text" value="' + rngA[i][j] + '" size="10" onChange="updateSS(' + i + ',' + j + ');" />' + '</th>';
      }
    }
    s+='</tr>';
  }
  s+='</table>';
  //s+='<div id="success"></div>';
  s+='</body></html>';
  switch (mode)
  {
    case 'dialog':
      var userInterface=HtmlService.createHtmlOutputFromFile('htmlss').setWidth(1000).setHeight(450);
      userInterface.append(s);
      SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Spreadsheet Data for ' + ss.getName() + ' Sheet: ' + sht.getName());
      break;
    case 'web':
      var userInterface=HtmlService.createHtmlOutputFromFile('htmlss').setWidth(1000).setHeight(450);
      return userInterface.append(s).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
   }
}

function updateSpreadsheet(i,j,value)
{
  var ss=SpreadsheetApp.openById(SSID);
  var sht=ss.getSheetByName(sheetName);
  var rng=sht.getDataRange();
  var rngA=rng.getValues();
  rngA[i][j]=value;
  rng.setValues(rngA);
  var data = {'message':'Cell[' + Number(i + 1) + '][' + Number(j + 1) + '] Has been updated', 'ridx': i, 'cidx': j};
  return data;
}

function doGet()
{
  var output=htmlSpreadsheet('web');
  return output;
}

htmlss.html:

<!DOCTYPE html>
<html>
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
    $(function() {

    });
    function updateSS(i,j)
    {
      var str='#txt' + String(i) + String(j);
      var value=$(str).val();
      $(str).css('background-color','#ffff00');
      google.script.run
         .withSuccessHandler(successHandler)
         .updateSpreadsheet(i,j,value)
    }
    function successHandler(data)
    {
      $('#success').text(data.message);
      $('#txt' + data.ridx + data.cidx).css('background-color','#ffffff');
    }
    console.log('My Code');
    </script>
    <style>
      th{text-align:left}
    </style>
  </head>
  <body>
  <div id="success"></div>

Upvotes: 1

Related Questions