我希望比较 Google 表格中的日期,然后返回多个条件匹配的最后/最新日期。
这就是我正在尝试做的事情......
...从 C 列(蓝色列)中查找最后/最新发布日期,其中 F、H、J 和 L 列(橙色列)全部与所有其他行中的相同列单元格匹配。然后返回最后/最新日期并将其填写到 D 列中。
在此演示表中... https://docs.google.com/spreadsheets/d/1WgI8lr3y-68n7jzbcinG-s-YAmRXFrTg-K8SlDG98jM/
在该描述和演示表之间,我希望这是有意义的。
我确实在这里找到了另一个类似的请求(但无法理解如何使其适应我自己的需求)...... Google 表格 - 比较日期并返回符合要求的最新行
任何帮助将不胜感激。谢谢!
您可以在 Google Apps 脚本的帮助下,在专为您的用例设计的 Google 表格中创建自定义函数。要访问 Apps 脚本编辑器,请从电子表格中转到扩展 > Apps 脚本。
有关此方法的一些要点:
您可以像通常在 Google 表格上一样使用此函数或公式,但它将执行您的用例中定义的一组流程(“比较 Google 表格中的日期,然后返回多个条件匹配的最后/最新日期” ”)。
在脚本中,您需要输入5个范围:参考列(要获取日期的位置),以及需要检查是否匹配的4列。供您参考,自定义公式如下所示:
=returnDateFromCriteria(ran_ref, ran1, ran2, ran3, ran4)
请注意,您可以在 Apps 脚本编辑器中将此函数重命名为您喜欢的函数名称。但是,重命名函数中的任何参数可能需要您调整脚本本身中的值,以使其发挥相同的作用。
至于脚本本身,您可以参考下面的示例脚本供您参考:
/**
* A custom formula function that returns the last entered date on
* a specific column, based on if a match is found from multiple criteria
* across other columns
* @param {ran_ref} Reference column range from where the dates will be extracted
* @param {ran1, ran2, ...} The column range/s where the criteria are to be checked for a match
* @return A resulting list of dates which have already been sorted and matched
* @customfunction
*/
function returnDateFromCriteria(ran_ref, ran1, ran2, ran3, ran4) {
// filters all blank values, and reduces the retrieved values into their respective 1D arrays
// note: this is needed for the matching later in the script
var match1 = ran1.filter(x => x != "").flat();
var match2 = ran2.filter(x => x != "").flat();
var match3 = ran3.filter(x => x != "").flat();
var match4 = ran4.filter(x => x != "").flat();
var ref = ran_ref.filter(x => x != "").flat();
var res = [];
var ctr = 0;
// this is to re-arrange the date format from the spreadsheet into the month/day/year format
// since Google Apps Script retrieves both the date and time as its value by default
for (i = 0; i < match3.length; i++) {
match3[i] = Utilities.formatDate(match3[i], 'America/New_York', 'MM/dd/yyyy');
ref[i] = Utilities.formatDate(ref[i], 'America/New_York', 'MM/dd/yyyy');
}
// the full script to update the dates for matching rows
for (i = 0; i < ref.length; i++) {
// if two rows have identical values across the defined column ranges
if ((match1[i] == match1[i + 1]) && (match2[i] == match2[i + 1]) && (match3[i] == match3[i + 1]) && (match4[i] == match4[i + 1])) {
// this if statement checks if an active match exists
// if so, it updates the date already pushed in the resulting array
// with the last value retrieved
if (ctr) {
res[i - 1] = ref[i + 1];
}
// if a match is just detected, the last value is pushed onto the resulting array
res.push(ref[i + 1]);
ctr++;
}
// terminates the existing match, to be able to check for other matches
else if (ctr) {
res.push(ref[i]);
ctr = 0;
}
// if a row is unique amongst other values
else {
res.push(ref[i]);
}
}
// this returns the resulting array back to the spreadsheet
// reformatted into a 2D array (as a spreadsheet cell is 2D, with a defined row and column)
return res.map(x => [x]);
}
保存此脚本后,您可以继续在 Google Sheets 上正常使用该公式。
自定义函数脚本中使用的函数可以参考以下链接: