Phr*_*ogz 2 formulas google-sheets
使用 Google 表格,我想像这样自动对行进行编号:
关键是我希望它只使用内置函数。
我有一个实现,其中子项位于单独的列中(例如,“Foo”在 B 列中,“Bar”在 C 列中,“Baz”在 D 列中)。但是,它使用自定义 JavaScript 函数,并且计算自定义 JavaScript 函数的速度很慢,加上依赖项,可能还加上 Internet 连接速度较慢,这意味着我的解决方案每行可能需要超过一秒钟 (!) 来计算。
作为参考,这是我的自定义函数(我想放弃它以支持本机代码):
/**
* 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(".");
}
}
}
Run Code Online (Sandbox Code Playgroud)
例如,单元格 A7 将使用公式:
=WBS_ID(A$2:A6,B7:D7)
...产生结果“1.3.2”
请注意,在上面的示例中,在编号过程中会跳过空白行。不遵守这一点的答案 - 其中 ID 是根据 ) 确定性计算的ROW()
- 是可以接受的(甚至可能是可取的)。
编辑:是的,我自己尝试过这样做。我有一个使用三个额外列的解决方案,我选择不包含在问题中。我已经在 Excel 中编写方程至少 25 年(以及 1 年的 Google 电子表格)。我已经浏览了 Google 电子表格的功能列表,但没有一个让我想到使我以前没有想到的东西成为可能。
当问题是编程问题并且问题是无法看到如何从 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,),))))))
Run Code Online (Sandbox Code Playgroud)
在我的辩护中,我在工作中度过了漫长的一天 - 使本来应该是简单的事情复杂化似乎是我今天的事情:)
归档时间: |
|
查看次数: |
1450 次 |
最近记录: |