简体   繁体   中英

if row contains specific text then build average google apps scripts (google sheets) (Javascript)

I dont know how to program a code in google sheets wether in the sheet or in the script-tool using google apps scripts.

Example:

January 2017|February 2017|March 2017|April 2017|...
----------
Numberscolumn |Numberscolumn|

----------

I want to build a Average only of columns that have the word "January" oder "April" for example. How can I put the right code ?

Example:

=IF(TRUE;SEARCH("January "&" =ISNUMBER"; A2:A20))

Or maybe write an Macro in Google Apps Scripts ?

If the headers are text rather than actual dates, this should work for you

=average(filter(transpose(A2:F),regexmatch(transpose(A1:F1),"January|April")))

在此处输入图片说明 If they are actual dates, then

=average(filter(transpose(A2:F),(transpose(A1:F1)=datevalue("January 2017"))+(transpose(A1:F1)=datevalue("April 2017"))))

Or to match on month and not specify a year

=AVERAGE(filter(transpose(A2:F),(text(transpose(A1:F1),"MMMM")="January")+(text(transpose(A1:F1),"MMMM")="April")))

Or more concisely

=AVERAGE(filter(transpose(A2:F),regexmatch(text(transpose(A1:F1),"MMMM"),"January|April")))

在此处输入图片说明

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