0

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

GPni
  • 143
  • 1
  • 14
  • Your input formats are invalid - I'm assuming you mean strings? Or maybe something else... – Jack Bashford Aug 26 '19 at 13:35
  • Yes strings I guess. Thank you – GPni Aug 26 '19 at 13:36
  • Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – TheMaster Aug 26 '19 at 14:12
  • i think its not a duplicate since in my case I need to locate the value in the google sheet and change its value. My apologies. I'm still new in programming – GPni Aug 26 '19 at 15:08

2 Answers2

2

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.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
0

How about:

var result = timeValues.map((_tv) => { return [_tv]; });
SebAxeon
  • 5
  • 5
  • I got a syntax error. Sorry I'm not that good in using `map` method. so I'm still searching for it. – GPni Aug 26 '19 at 15:10