使用原生功能计算Google表格的分层标签 [英] Calculate hierarchical labels for Google Sheets using native functions

查看:216
本文介绍了使用原生功能计算Google表格的分层标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google表格,我想自动为行编号,如下所示:





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

>

我有一个实现工作,其中的子项在不同的列中(例如Foo在B列中,Bar在C列中,Baz在D列中)。但是,它使用自定义JavaScript函数,并且评估自定义JavaScript函数的慢速方式以及依赖项(可能与慢速Internet连接相结合)意味着我的解决方案每行可能需要超过一秒(!)才能计算。






作为参考,这里是我的自定义函数(我想放弃支持本地代码):

/ **
*计算此行的工作分解结构ID。
*
* @param {range} priorIds在此之前的ID。
* @param {range}名称此行的名称。
* @return WBS字符串标识(如2.1.5)或空字符串(如果没有名称)。
* @customfunction
* /
函数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]; (lastId)pieces =(lastId +)。split('。')。map(function(s){return s * 1});
if(lastId)pieces =
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(。);



code $


例如,单元格A7会使用公式:

= WBS_ID(A $ 2:A6,B7:D7)


...以产生结果1.3.2




请注意,在上面的例子中,空白行在编号时被跳过。可以接受(甚至可取的)一个答案,即不承认这种情况 - 其中ID是通过 ROW())确定性地计算的。






编辑:是的,我试图自己做这个。我有一个解决方案,使用了三个额外的列,我选择不包括在问题中。我一直在Excel中编写方程至少25年(Google Spreadsheets为期1年)。我已经浏览了谷歌电子表格的功能列表,并且他们中没有一个能够跳出来,因为我可以做出以前没有想到的东西。



当问题是一个编程问题,问题是无法看到如何从A点到B点,我不知道展示我所做的事情是有用的。我考虑过按时段分裂。我查找了一个 map 等价函数。我知道如何使用 isblank() counta()

解决方案

大声笑这是最长的(也许是最不必要的复杂方式来结合公式),但是因为我认为它确实有效是有趣的,只要您只需在第一行中添加1,然后在第二行添加:

  = if(row()= 1, 1,如果(和(ISTEXT(D2),COUNTA(分裂(A1))= 3),左(α-1,4)及 ; N(右(A1,1)+1),如果(和( ISBLANK(B2),ISBLANK(C2),ISBLANK(D2)) ,如果(和(ISBLANK(B2),ISBLANK(C2),ISNUMBER(间接的(地址(行() -  1,列())) )),间接的(地址(行() -  1,列()))&安培;&安培;如果(ISTEXT(D2),圆形(最大(间接(地址(1,列())及 。: &安培;地址(行() -  1,列())))+ 0.1,)),如果(和(ISBLANK(B2),ISTEXT(C2)),轮(MAX(间接的(地址(1,列( ))及 : &安培;地址(行() -  1,列())))+ 0.1,2),如果(ISTEXT(B2),圆形(最大(间接(地址(1,列()) &:& address(row() -  1,column())))+ 1,),))))))


一个很长的一天工作 - 复杂的应该是一件简单的事情今天似乎是我的事:)


Using Google Sheets, I want to automatically number rows like so:

The key is that I want this to use built-in functions only.

I have an implementation working where child items are in separate columns (e.g. "Foo" is in column B, "Bar" is in column C, and "Baz" is in column D). However, it uses a custom JavaScript function, and the slow way that custom JavaScript functions are evaluated, combined with the dependencies, possibly combined with a slow Internet connection, means that my solution can take over one second per row (!) to calculate.


For reference, here's my custom function (that I want to abandon in favor of native code):

/**
 * 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(".");
    }
  }
}

For example, cell A7 would use the formula:
=WBS_ID(A$2:A6,B7:D7)
...to produce the result "1.3.2"


Note that in the above example blank rows are skipped during numbering. An answer that does not honor this—where the ID is calculated determinstically from the ROW())—is acceptable (and possibly even desirable).


Edit: Yes, I've tried to do this myself. I have a solution that uses three extra columns which I chose not to include in the question. I have been writing equations in Excel for at least 25 years (and Google Spreadsheets for 1 year). I have looked through the list of functions for Google Spreadsheets and none of them jumps out to me as making possible something that I didn't think of before.

When the question is a programming problem and the problem is an inability to see how to get from point A to point B, I don't know that it's useful to "show what I've done". I've considered splitting by periods. I've looked for a map equivalent function. I know how to use isblank() and counta().

解决方案

Lol this is hilariously the longest (and very likely the most unnecessarily complicated way to combine formulas) but because I thought it was interesting that it does in fact work, so long as you just add a 1 in the first row then in the second row you add:

=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,),))))))

in my defense ive had a very long day at work - complicating what should be a simple thing seems to be my thing today :)

这篇关于使用原生功能计算Google表格的分层标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆