google-sheets - 使用 native 函数计算 Google 表格的层次标签

标签 google-sheets formulas

使用 Google 表格,我想像这样自动对行编号:

Hierarchy of items preceded by column of IDs, e.g. 1, 1.1, 1.1.1, 1.1.2

关键是我希望它只使用内置函数

我有一个实现,其中子项位于单独的列中(例如,“Foo”在 B 列中,“Bar”在 C 列中,“Baz”在 D 列中)。但是,它使用自定义 JavaScript 函数,自定义 JavaScript 函数的评估方式较慢,加上依赖关系,可能再加上较慢的 Internet 连接,这意味着我的解决方案每行可以占用一秒钟 (!) 来计算。


作为引用,这是我的自定义函数(我想放弃以支持 native 代码):

/**
 * Calculate the Work Breakdown Structure id for this row.
 *
 * @param {range}  priorIds  IDs that precede this one.
 * @param {range}  names     The names for this row.
 * @return A WBS string id (e.g. "2.1.5") or an empty string if there are no names.
 * @customfunction
 */
function WBS_ID(priorIds,names){
  if (Array.isArray(names[0])) names = names[0];
  if (!names.join("")) return "";
  var lastId,pieces=[];
  for (var i=priorIds.length;i-- && !lastId;) lastId=priorIds[i][0];
  if (lastId) pieces = (lastId+"").split('.').map(function(s){ return s*1 });
  for (var i=0;i<names.length;i++){
    if (names[i]){
      var s = pieces.concat();
      pieces.length=i+1;
      pieces[i] = (pieces[i]||0) + 1;
      return pieces.join(".");
    }
  }
}

例如,单元格 A7 将使用以下公式:
=WBS_ID(A$2:A6,B7:D7)
...产生结果“1.3.2”


请注意,在上面的示例中,空白行在编号期间被跳过。不接受这一点的答案(ID 是根据 ROW()) 确定地计算的)是可以接受的(甚至可能是可取的)。


编辑:是的,我自己尝试过这样做。我有一个解决方案使用三个额外的列,我选择不包括在问题中。我用 Excel 写方程至少 25 年了(用 Google 电子表格写了 1 年)。我查看了 Google Spreadsheets 的函数列表,但没有一个能使我以前没有想到的事情成为可能。

当问题是编程问题并且问题是无法看到如何从 A 点到达 B 点时,我不知道“展示我做了什么”是否有用。我考虑过按期拆分。我一直在寻找一个 map 等效函数。我知道如何使用 isblank()counta()

最佳答案

大声笑这是最长的(并且很可能是组合公式的最不必要的复杂方法)但是因为我认为它确实有效很有趣,只要你只在第一行添加一个 1 然后在您添加的第二行:

=if(row()=1,1,if(and(istext(D2),counta(split(A1,"."))=3),left(A1,4)&n(right(A1,1)+1),if(and(isblank(B2),isblank(C2),isblank(D2)),"",if(and(isblank(B2),isblank(C2),isnumber(indirect(address(row()-1,column())))),indirect(address(row()-1,column()))&"."&if(istext(D2),round(max(indirect(address(1,column())&":"&address(row()-1,column())))+0.1,)),if(and(isblank(B2),istext(C2)),round(max(indirect(address(1,column())&":"&address(row()-1,column())))+0.1,2),if(istext(B2),round(max(indirect(address(1,column())&":"&address(row()-1,column())))+1,),))))))

enter image description here

在我看来,我工作了很长的一天 - 把本来应该很简单的事情复杂化似乎是我今天的事:)

关于google-sheets - 使用 native 函数计算 Google 表格的层次标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35711081/

相关文章:

google-sheets - 在 Google 表格中,如何查找包含特定文本的单元格的行号?

google-apps-script - 应用脚本条件格式按名称应用于工作表

google-apps-script - 如何使用 Google Apps 脚本从 Google 电子表格单元格获取 url?

google-sheets - 获取 Google 表格列中的最后一个非空单元格

excel - 你如何在Excel中平均最好的两次

多个工作表上的 Excel 方案管理器

google-apps-script - 如何复制电子表格并将其保存到特定文件夹?

regex - 使用 Regexextract 从 Google 表格中的文本中提取日、月、2 位数字年份

java - 在 JAVA 公式中使用 If 语句

regex - 如何规范/关联Google表格中的Unicode字符?