简体   繁体   English

使用Google Apps脚本查找二维数组

[英]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" 并更改其值"Not Available"

The array log are the values from google sheet using this code: 数组日志是使用以下代码的Google工作表中的值:

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) : 我尝试使用此代码(如您在上面的Google脚本代码中所见)

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"? 您对我如何搜索二维数组中的值然后转到其在Google工作表中的位置并将其值更改为“不可用”有任何建议或解决方案吗? 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 这是我的Google工作表的链接: https : //docs.google.com/spreadsheets/d/1lEfzjG1zzJVPMN8r-OpeZm6q9_IqSwk9DNCEY-q7ozY/edit?usp=sharing

The output from your timeValues are strings. timeValues的输出是字符串。 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: 如果[8:00 AM, 9:00 AM]的内容也是字符串,则可以通过以下方式比较它们并将符合条件的单元格值设置为“不可用”:

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". 基本上,您遍历两个数组,并且当内容一致时,将各自的timeValues条目替换为“ Not Available”。 After exiting the loops you assign to your range the values of the updated timeValues array. 退出循环后,您可以为范围分配更新的timeValues数组的值。

怎么样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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