简体   繁体   中英

How to SUMIF (from Google Sheets) using Google Apps Script?

Google Sheets has this function called SUMIF(), which I'm using inside an IF statement:

=IF(SUMIF(January!$F$6:$F,$B2,January!$G$6:$G)=0,"",SUMIF(January!$F$6:$F,$B2,January!$G$6:$G))

The IF is just to show an empty cell in case the value = 0

January!$F$6:$F is a column with names

$B2 is the cell with the name to compare against January!$F$6:$F

January!$G$6:$G is a column with numerical values

I'm using this function in many places, and each time I have to update a value is quite painful.

So I would to create my own function using the script editor , parsing the three values mentioned above:

=myOwnFunction(columnNames,columnValues,names)


Edited

There's another post asking a similar question, but he is working on a Macro, so when I tried to apply the same rules such as setValue I get a restriction error.



Can I get some help please?

Thanks in advance!

The question was already discussed earlier .

Now I can suggest something similar:

function myOwnFunction(columnNames, columnValues, names) {
  var res = 0;
  for (var i in columnNames) {
    if (columnNames[i][0] == names) res += columnValues[i][0];
  }
  if (res == 0) res = '';
  return res;
}

Note, I've used your own function name. But traditionally custom function names are uppercase, such as MY_OWN_FUNCTION.

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