根据单元格值分发数值

huangapple go评论58阅读模式
英文:

Distributing Values based on Cell value

问题

I am trying to use the most efficient method to distribute values based on J13. In this example, the value in J13 is three. I want the values in the range J4:J12 to basically add "1" to each cell until the total sum equals J13.

I'm using this formula, which gets the job done, but I have other columns where I want to do the same thing, and it will take a long time to swap cell references. Any help would be appreciated!

=iferror(IFS(j13=1,0,j13=2,1,j13=3,1,j13=4,1,j13=5,1,j13=6,1,j13=7,1,j13=8,1,j13=9,1,j13=10,1,j13=11,2,j13=12,2,j13=13,2,j13=14,2,j13=15,2))

英文:

根据单元格值分发数值

I am trying to use the most efficient method to distribute values based on J13. In this example the value in J13 is three. I want the values in range J4:J12 to basically add "1" to each cell until the total sum = J13

I'm using this which gets the job done but i have other columns where I wanna do this same thing and it will take a long time to swap cell references.... any help would be appreciated!

> =iferror(IFS(j13=1,0,j13=2,1,j13=3,1,j13=4,1,j13=5,1,j13=6,1,j13=7,1,j13=8,1,j13=9,1,j13=10,1,j13=11,2,j13=12,2,j13=13,2,j13=14,2,j13=15,2))

答案1

得分: 2

=WRAPCOLS(SEQUENCE(J13,1,1,0),ROWS(J3:J13)-1,0)

=BYROW(WRAPCOLS(SEQUENCE(J13,1,1,0),ROWS(J3:J13)-1,0), LAMBDA(r,SUM(r)))

英文:

Complementing Giselle explanation, you can use SEQUENCE inside WRAPCOLS to create "virtually" a range like this:

根据单元格值分发数值

=WRAPCOLS(SEQUENCE(J13,1,1,0),ROWS(J3:J13)-1,0)

With SEQUENCE repeating 1 and WRAPCOLS making as many columns as necessary. WRAPCOLS will count the rows in between the starting cell and the end cell; so if you add one more row or delete one in between the formula will keep working.

Then, you just insert it into BYROW to sum those 1s row by row:

=BYROW(WRAPCOLS(SEQUENCE(J13,1,1,0),ROWS(J3:J13)-1,0),  LAMBDA(r,SUM(r)))

根据单元格值分发数值

答案2

得分: 1

你可以在Google表格中使用序列函数

类似于这样的代码:

=SEQUENCE(J13,1,1,0) 

基本公式是:

=SEQUENCE(行数, 列数, 起始值, 步长)
  • 行数 是要返回的行数,因此在这种情况下是 J13 中的值。
  • 列数 是要返回的列数,在这种情况下,因为您只想使用列 J,所以它将是 1
  • 起始值 是序列开始的数字,对于这种情况是 1
  • 最后,步长 是序列中每个数字的增加/减少量,因为您只想添加数字 1,所以增量是 0

这是一个示例:

根据单元格值分发数值

英文:

You can use the Sequence function in Google Sheet

Something like this:

=SEQUENCE(J13,1,1,0) 

The base formula is:

=SEQUENCE(rows, columns, start, step)
  • The rows is the number of rows to return, so in this case the value in J13.
  • And the columns is the number of columns to return, for this case since you only want to use column J, it will be 1.
  • The start is the number to start the sequence at, for this case is 1.
  • And lastly, step is the amount to increase/decrease each number in the sequence, since you want to add number ones only. The increase will be 0.

Here is an example:

根据单元格值分发数值

答案3

得分: 1

根据您对之前答案的评论,您希望如果J13中的数字足够大,J4:J12中的数字可以"换行"(但您应该在原始问题中明确提到这一点,因为这一点并不明显),您可以尝试在J4中使用以下公式:

=byrow(wrapcols(sequence(J13,1,1,0),rows(J4:J12),),lambda(row,sum(row)))

我们正在创建一个由J13中的数字决定长度的1列,每9行(基于J4:J12数组中的行数)包装一次此数组,然后跨行求和以实现所需的行为。

英文:

Based on your comment to the previous answer that you would like the numbers in J4:J12 to 'wrap' if the number in J13 is big enough (which you should really have included in the original question as this was not obvious at all), you can try the following formula in J4:

=byrow(wrapcols(sequence(J13,1,1,0),rows(J4:J12),),lambda(row,sum(row)))

We are creating a column of 1s of length dictated by the number in J13, wrapping this array every 9 rows (based on the number of rows in the J4:J12 array) to make a 2D array, then summing across rows to achieve the desired behaviour.

huangapple
  • 本文由 发表于 2023年4月10日 23:39:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定