0
votes

I'm very new to Google App Script so please bear with me. I'm trying to create custom functions in Google Sheets to monitor monthly and weekly sales and expenses. I can do it with built-in formulas, but it's too time-consuming to edit every cell every time. Here is what the Google Sheet looks like.

For monthly accounting, I use the formula below (here it's for "Sales"). I use the sum of multiple SUMIFS for the expenses since there are multiple criteria ("Supplies", "Staff", or "Operations").

=ARRAYFORMULA(SUMIFS('2019'!$F$2:$F,text('2019'!$A$2:$A,"MMM"),A3,'2019'!$E$2:$E,"Sales"))

For weekly accounting, I use another formula below. Here, I sum up all transactions (here "Sales") within a given range of dates (here Mar 31 2019 to Apr 6 2019). As in above, I use the same principle of summing up multiple SUMIFS functions for "Expenses".

Sales: =SUMIFS('2019'!$F$2:$F,'2019'!$A$2:$A,">="&date(2019,3,31),'2019'!$A$2:$A,"<="&date(2019,4,6),'2019'!$E$2:$E,"Sales")

Expenses: =SUMIFS('2019'!$F$2:$F,'2019'!$A$2:$A,">="&date(2019,3,31),'2019'!$A$2:$A,"<="&date(2019,4,6),'2019'!$E$2:$E,"Supplies")+SUMIFS('2019'!$F$2:$F,'2019'!$A$2:$A,">="&date(2019,3,31),'2019'!$A$2:$A,"<="&date(2019,4,6),'2019'!$E$2:$E,"Staff")+SUMIFS('2019'!$F$2:$F,'2019'!$A$2:$A,">="&date(2019,3,31),'2019'!$A$2:$A,"<="&date(2019,4,6),'2019'!$E$2:$E,"Operations")

How can I translate the formulas shown above into a custom function? I was trying to write something like this one:

enter image description here

1
Where is your code?TheMaster
It should be in the Google Sheet I linked. But it's very bare.ajthealchemist
Giving view only rights to your sheet prevents peeps from accessing Script Editor. You will get more help just posting your code separately. (Also try identify where you think the problem might be.)New_2_Code
I'd like to suggest using a Bkper function. It makes your Sheet much simpler and cleaner If you have a Sales group on your book you can use the function =BKPER_BALANCES_PERIOD("bookid", 1, “group:'Sales' after:01/2019 before:01/2020”, TRUE, FALSE). bkper functions for Google SheetsJacobvdb

1 Answers

0
votes

I suggest a different strategy that avoids custom functions and allows you to use standard functions without complex parameters and, in the process, achieves a more logical layout of the data.

  • 2019 Sheet: Category (Column E): make these a dropdown selection to avoid typos and other errors.
  • 2019 Sheet: Column F - It's not clear whether sales should be normally negative (credit), or expenses should be normally positive (debit). Either way, the sum of this column will reconcile to something at some stage so the "sign" of transactions is important. It also means that formulas for Sales on the Summary Sheet should be prefixed with a negative (so that they appear as a positive number).
  • Summary Sheet-Monthly Sales - Prefix with a "minus" so that sales are shown as positive. =-ARRAYFORMULA(SUMIFS('2019'!$F$2:$F,text('2019'!$A$2:$A,"MMM"),A3,'2019'!$E$2:$E,"Sales"))
  • **Summary Sheet: Monthly Expenses"" - Simplify the formula: just sum the values that are NOT "Sales" =ARRAYFORMULA(SUMIFS('2019'!$F$2:$F,text('2019'!$A$2:$A,"MMM"),A3,'2019'!$E$2:$E,"<>Sales"))
  • Summary Sheet: Net - =B3-C3 (Sales minus Expenses)
  • Summary Sheet: Weekly reporting - It is always difficult to balance monthly results against weekly results. So much so, that many business adopt a quarterly cycle of 4/4/5 weeks to cater for the changes in the number of days per month. In any event, the formula that you are using is VERY complicated and prone to error through typos (if nothing else). I suggest:
  • create columns for the respective weekly "From"/"To" dates on the weekly report, and use date arithmetic to calculate the respective dates for each new week/month.
  • use Weekly Sales and Expenses formula that reference the "From/To" dates. This makes your formulas completely generic, enables you to copy formula with conventional "Copy/Paste" commands, and doesn't require detailed formula editing.
  • Consider whether you want the weekly results to align to the monthly results. If you do, then
    • fine-tune the month-end by adding or deleting days to the "To" date in week#4. The Week#1 "From" date for the following month will adjust automatically, as will the respective "From"/"To" dates for the other weeks in the following month(s). This will help eliminate unintentional errors such as including March 31 in both March week#4 and April week#1.
    • You might also add an extra three columns at the right to sum Total Sales, Total Expenses and Net profit for the four weeks. This figure will agree to the monthly result.

Formula simplification

By disclosing the From and To dates, you can use those dates in your SumIFS formula. For example, March Week#1:
Sales: =-SUMIFS('2019'!$F$2:$F,'2019'!$A$2:$A,">="&G3,'2019'!$A$2:$A,"<="&H3,'2019'!$E$2:$E,"Sales") Expenses: =SUMIFS('2019'!$F$2:$F,'2019'!$A$2:$A,">="&G3,'2019'!$A$2:$A,"<="&H3,'2019'!$E$2:$E,"<>Sales")


Monthly Results
Monthly Results

Sales: =-ARRAYFORMULA(SUMIFS('2019'!$F$2:$F,text('2019'!$A$2:$A,"MMM"),A3,'2019'!$E$2:$E,"Sales"))
Expenses: =ARRAYFORMULA(SUMIFS('2019'!$F$2:$F,text('2019'!$A$2:$A,"MMM"),A3,'2019'!$E$2:$E,"<>Sales"))
Net: =B3-C3


Weekly results - extract
Weekly Results (Extract)

Week#1 Results
Sales: =-SUMIFS('2019'!$F$2:$F,'2019'!$A$2:$A,">="&F3,'2019'!$A$2:$A,"<="&G3,'2019'!$E$2:$E,"Sales")
Expenses: =SUMIFS('2019'!$F$2:$F,'2019'!$A$2:$A,">="&F3,'2019'!$A$2:$A,"<="&G3,'2019'!$E$2:$E,"<>Sales")
Net: =H3-I3
Week#2Results
Sales: =-SUMIFS('2019'!$F$2:$F,'2019'!$A$2:$A,">="&K3,'2019'!$A$2:$A,"<="&L3,'2019'!$E$2:$E,"Sales")
Expenses: =SUMIFS('2019'!$F$2:$F,'2019'!$A$2:$A,">="&K3,'2019'!$A$2:$A,"<="&L3,'2019'!$E$2:$E,"<>Sales")
Net: =M3-N3


Date Arithmetic Full Weekly Results

March Week#1 From: Mar 1 2019 (the only date that is entered manually)
March Week#1 To: =F3+6
March Week#2 From: =G3+1
March Week#2 To: =K3+6
March Week#4 To: =U3+6+3
April Week#1 From: =V3+1