【问题标题】:How to match a string above a given cell index如何匹配给定单元格索引上方的字符串
【发布时间】:2021-11-08 21:41:43
【问题描述】:

我有一个这样的时间表,不知怎么确定了爱丽丝在B7中的位置。

给定索引B7,我怎样才能找到匹配的日期?这里是 Alice 上方的第一个以 October 开头的单元格。

【问题讨论】:

标签: google-sheets google-sheets-formula


【解决方案1】:

建议

鉴于每个日期的名称总数是随机的(某些日期只有 2 个名称,而其他日期有 3 个基于您的样品表)。

您可以尝试为此方法使用自定义公式,只需将下面的示例函数作为bound script 复制并粘贴到您的工作表文件中,然后调整您的设置:

function FINDDATE(cell) {
  var sheet;
  if(cell.split("!").length==1){
    sheet = SpreadsheetApp.getActive().getActiveSheet();
  }else{
    sheet = SpreadsheetApp.getActive().getSheetByName(cell.split("!")[0]);
  }
  var selectedLoc = sheet.getRange(cell);
  for(i=selectedLoc.getRow();  i != 1; i--){
    var dateFound = isValidDate(sheet.getRange(i,selectedLoc.getColumn()).getValue());
    if(dateFound == true){ //Return the date of the very first detected as date above the index
      var res = Utilities.formatDate(sheet.getRange(i,selectedLoc.getColumn()).getValue(), Session.getScriptTimeZone(), "MMMM d");
      return res.toString();
    }
  }
}

function isValidDate(d) { //Check if a cell is a date, reference from https://stackoverflow.com/a/17110249
  if ( Object.prototype.toString.call(d) !== "[object Date]" )
    return false;
  return !isNaN(d.getTime());
}

示例演示:

FINDDATE自定义函数的使用方法有以下两种:

方法一:

方法二:

方法 3: 在不同的工作表上使用 FINDDATE

【讨论】:

  • 太好了,这看起来对我的情况有用。但是,我正在使用不同的工作表(不认为这很重要)并且不熟悉该脚本语言 - 是否容易适应以便我可以在另一个工作表中使用它,例如致电=FINDDATE("Schedule!B7")
  • 太棒了。我刚刚更新了我的答案,它现在将支持声明一个特定的工作表名称,如=FINDDATE("Schedule!B7")。我已经展示了一个示例图片供您在我的回答中参考。
  • 做到了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2016-05-16
  • 2019-10-10
  • 2013-01-19
  • 2021-08-30
  • 2020-11-22
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
相关资源
最近更新 更多