简体   繁体   中英

Find inside 2 dimensional Array Using Google Apps Script

I have values in an array which are:

[8:00 AM, 9:00 AM] //result of another function

And I would like to find their values inside this 2-Dimensional Array:

[
 [8:00 AM], 
 [9:00 AM], 
 [10:00 AM], 
 [11:00 AM], 
 [12:00 NN], 
 [1:00 PM], 
 [2:00 PM], 
 [3:00 PM]
]

and change the their values "Not Available"

The array log are the values from google sheet using this code:

function testRow(){

  var lookDate = "Aug 28, 2019";
  var ss = SpreadsheetApp.openByUrl(url); 
  var ts = ss.getSheetByName("Time_Select");
  var checkData = ts.getRange(1, 1, 1, ts.getLastColumn()).getDisplayValues()[0];

  var index = checkData.indexOf(lookDate)+1;

  var timeValues = ts.getRange(2, index, ts.getLastRow()-1, 1).getValues();
  Logger.log(timeValues)

  /*var checkSplit = dateValues.join().split(",");
  var checkMe = checkSplit.indexOf(dataDisable[1]);
  var timeValues = ts.getRange(checkMe, index).getValue();*/
}

I tried to use this code (as you can see in the above google script code) :

var checkSplit = dateValues.join().split(",");
var checkMe = checkSplit.indexOf(dataDisable[1]);
var timeValues = ts.getRange(checkMe, index).getValue();

but it was giving me a wrong value. Do you have any suggestions or solutions on how can I search the values inside the 2 dimensional array then go to its location in google sheet and change its value to "Not Available"? Thank you very much in advance for the help.

here is the link for my google sheet: https://docs.google.com/spreadsheets/d/1lEfzjG1zzJVPMN8r-OpeZm6q9_IqSwk9DNCEY-q7ozY/edit?usp=sharing

The output from your timeValues are strings. If the contents of [8:00 AM, 9:00 AM] are also strings, this is how you can compare them and set the rescpective cell values to "Not Available" in case of coincidence:

function testRow(){

  var lookDate = "Aug 28, 2019";
  var ss = SpreadsheetApp.openByUrl(url); 
  var ts = ss.getSheetByName("Time_Select");
  var checkData = ts.getRange(1, 1, 1, ts.getLastColumn()).getDisplayValues()[0];

  var index = checkData.indexOf(lookDate)+1;
 // I called your array resulting from another function 'times' 
  var times=['8:00 AM', '9:00 AM'];
  var timeValues = ts.getRange(2, index, ts.getLastRow()-1, 1).getValues();
  for(var i=0;i<times.length;i++){
    for(var j=0;j<timeValues.length;j++){
      if(times[i]==timeValues[j][0]){
        timeValues[j][0]="Not Available"
      }
    }
  }
 ts.getRange(2, index, ts.getLastRow()-1, 1).setValues(timeValues);
}

Basically, you loop through both arrays and when the contents coincide you replace the respective entry of timeValues with "Not Available". After exiting the loops you assign to your range the values of the updated timeValues array.

怎么样:

var result = timeValues.map((_tv) => { return [_tv]; });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM