Formulas in Numbers with Apple Script

Hi,


Hope you're all well.


My AppleScript learning is doing really well. Just a quick question about formulas using apple scripts with numbers.


A picture paints a thousand words so here's a picture:



  1. Is it possible to tell apple scripts to copy the formula from cell B7 (essentially B7 is the sum of B2:B6) to cell C7, so that I can get the sum of C2:C6? If so, how?
  2. Ideally this data is going to be put in monthly so how would be best to code apple scripts so that I don't have to change the script every month? eg. March D2:D6, April E2:E6 etc.


Thanks in advance!

MacBook Air 13", macOS 10.14

Posted on Mar 16, 2019 8:23 AM

Reply
5 replies

Mar 16, 2019 7:12 PM in response to Apple_Script

Here is something that automagically calculates the next month total formula, and assigns it to that total cell.


-- Based on current month name, construct new total formula for that column
-- and assign it to that month's total cell based on the following criteria:
-- 1) previous month has total value
-- 2) current month has row 2 (week 1) cell value that is non-zero.
-- Assumption: Numbers spreadsheet is already open in Numbers

-- reference: https://discussions.apple.com/thread/250236627
-- version: 1.0
-- tested: macOS High Sierra 10.13.6 (17G5019) with Numbers v5.3
-- VikingOSX, 2019-03-16, Apple Support Communities, No warranties at all.

property mname : month of (current date)

tell application "Numbers"
	tell front document
		tell active sheet's first table
			set {newFormula, prevSum, cell2, curSum} to my new_sum_formula(mname) as list
			if value of cell prevSum is 0 or value of cell prevSum is missing value then
				return
			end if
			if value of cell cell2 > 0 then
				set value of cell curSum to newFormula
			end if
		end tell
	end tell
end tell
return

on new_sum_formula(mname)
	-- returns:
	-- 1) new formula for current month totals
	-- 2) prevSum - cell address of previous months total
	-- 3) cell2 - current month columns cell 2
	-- 4) curSum - address of current months total cell
	-- alpha column letters for year of months
	set letters to "BCDEFGHIJKLM"
	set myindex to 0
	-- get the letter matching the index of the current month
	set alpha to text (myindex + (mname as integer)) of letters
	set curSum to alpha & 7 as text
	set cell2 to alpha & 2
	-- last month alpha
	set beta to text (myindex + (mname as integer) - 1) of letters
	set prevSum to beta & 7 as text
	return {"=SUM(" & alpha & 2 & ":" & alpha & 6 & ")", prevSum, cell2, curSum}
end new_sum_formula

Mar 21, 2019 3:54 AM in response to Apple_Script

I'd probably take advantage of Numbers scripting properties to do this a little more succinctly:


tell application "Numbers"
	tell front document
		tell active sheet's first table
			tell (row 7's first cell whose value is missing value)
				set colNam to its column's name --> e.g. "C"
				set its value to "=SUM(" & colNam & 2 & ":" & colNam & 6 & ")"
			end tell
		end tell
	end tell
end tell


Or, if one uses a Footer Row "Numbers style" which allows the formula =SUM(C) rather than =SUM(C2:C6) then the script becomes correspondingly simpler:


tell application "Numbers"
	tell front document
		tell active sheet's first table
			tell (row 7's first cell whose value is missing value)
				set colNam to its column's name --> e.g. "C"
				set its value to "=SUM(" & colNam & ")"
			end tell
		end tell
	end tell
end tell



The script looks for the first empty cell in row 7 to place the formula.


SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Formulas in Numbers with Apple Script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.