通过比较2个数组查找引用


function getResponseList () {
var listFormResponse = sheetNameFormResponse.getRange(2,columnOfDateTime(),sheetNameFormResponse.getLastRow()-1).getValues();
var lastRow = sheetName.getRange(2, 1).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var listAvaApointment = sheetName.getRange(2,1,lastRow).getValues();
var count = listFormResponse.filter(function(listFormResponse) {
return listFormResponse == listAvaApointment[0][0];
});
Logger.log(listFormResponse);
Logger.log(listAvaApointment[0][0]);
Logger.log(count);
}

我目前使用array.filter((.length通过比较两个数组来查找相关性。但是,filter((函数工作不正常,并且通过比较2个数组而不返回任何结果。

執行記錄
上午11:09:13  通知  開始執行
上午11:09:14  資訊  [[Tue Mar 29 15:00:00 GMT+08:00 2022], 
[Wed Mar 30 14:30:00 GMT+08:00 2022], 
[Wed Mar 30 16:00:00 GMT+08:00 2022], 
[Wed Mar 30 15:30:00 GMT+08:00 2022], 
[Wed Mar 30 14:30:00 GMT+08:00 2022], 
[Wed Mar 30 10:00:00 GMT+08:00 2022], 
[Wed Mar 30 10:00:00 GMT+08:00 2022], 
[Wed Mar 30 10:00:00 GMT+08:00 2022]]
上午11:09:14  資訊  Wed Mar 30 10:00:00 GMT+08:00 2022
上午11:09:14  資訊  []
上午11:09:14  通知  執行完畢
您正在将[]与日期对象进行比较。所以它不会返回匹配项。2022年3月30日星期三01:00:00 GMT-0400(东部夏令时(
function getResponseList () {
var listFormResponse = sheetNameFormResponse.getRange(2,columnOfDateTime(),sheetNameFormResponse.getLastRow()-1).getValues();
var lastRow = sheetName.getRange(2, 1).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var listAvaApointment = sheetName.getRange(2,1,lastRow).getValues();
var count = listFormResponse.filter(function(listFormResponse) {
return listFormResponse[0] == listAvaApointment[0][0];
});
Logger.log(listFormResponse);
Logger.log(listAvaApointment[0][0]);
Logger.log(count);
}

描述

我没有办法测试这个,因为我不知道你的数据是什么,但在当地它应该有效。我不知道你想测试listFormResponse的哪个元素。

listFormResponse是一个2D数组,所以在array.filter回调函数中,函数的参数是数组的行,我称之为formResponse。然后将formResponse与listAvaApointment数组进行比较,创建另一个数组,我将调用foundResponse,其中包含通过测试的每一行。

编写脚本

function getResponseList () {
var listFormResponse = sheetNameFormResponse.getRange(2,columnOfDateTime(),sheetNameFormResponse.getLastRow()-1).getValues();
var lastRow = sheetName.getRange(2, 1).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var listAvaApointment = sheetName.getRange(2,1,lastRow).getValues();
var foundResponse = listFormResponse.filter(function(formResponse) {
formResponse[0] == listAvaApointment[0][0];
});
Logger.log(listFormResponse);  // A 2D array
Logger.log(listAvaApointment[0][0]);
Logger.log(foundResponse.length); // A 2D array
}

参考

  • Array.filter((

最新更新