简体   繁体   中英

Function "length" not working properly in google apps script

I made function in the google sheet

function AVG_ZERO(input) {
  return input.length
};

But length of array different If the range of numbers is located in a column, then it is considered correct If the range of numbers is located in a string, then the length is 1 Links to images: https://postimg.cc/56xfmhZh https://postimg.cc/HVc13T6P

Try unfolding the array by using the Array.flat() function into separate elements regardless of the orientation:

function AVG_ZERO(input) {
  return input.flat().length
};

That will give you 3 for both row and column.

在此处输入图像描述

Issue:

If you pass a range of cells as an argument of a custom function, the argument will be treated by your custom function as a two-dimensional array of the cell's values, with the inner arrays being the individual rows. That is, the outer array length will equal the number of rows in your range: 1 for B1:D1 and 3 for B1:B3 .

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM