Quickly fill in blank cells (Excel equivalent)?

I often import reports from Quicken into Numbers to create data tables for analysis purposes and find that I must fill in blank cells to ensure I have data consistency across rows.


While I am confident there is no built in way for Numbers to quickly populate these blank cells, I am asking here if AppleScript has the potential of providing the means.


The following link gives insight into how it is done with Excel. Does the AppleScript library provide the tools for something similar?


http://www.techrepublic.com/blog/microsoft-office/quickly-fill-blank-cells-in-ex cel/


-DaverDee

MacBook Pro (15-inch Mid 2010), OS X Mavericks (10.9.3)

Posted on Mar 21, 2015 1:47 PM

Reply
10 replies

Mar 22, 2015 6:10 AM in response to DaverDee

Hi DaverDee,


Here is an AppleScript that does what you want in Numbers 3.


User uploaded file



tell application "Numbers"

tell front document to tell active sheet

tell (first table whose selection range's class is range)

tell selection range

set pv to ""

repeat with c in cells

set v to c's value

if v is not missing value then

set pv to v

else

set c's value to pv

end if

end repeat

end tell

end tell

end tell

end tell



To run:


  1. Copy-paste script into Script Editor
  2. Select range in Numbers where you want to fill in blanks.
  3. Click run button in Script Editor


If you want to run this from the menu rather than Script Editor then you can put it in the Numbers script menu (in Library > Scripts > Applications > Numbers) or in an Automator Service (where you could also attach a keyboard shortcut).


SG

Mar 22, 2015 12:01 PM in response to SGIII

Try this with Numbers 2.3:


tell application "Numbers"

tell front document

tell (first sheet whose tables's selection range's class contains range)

tell (first table whose selection range's class is range)

tell selection range

set pv to ""

repeat with c in cells

set v to c's value

if v is not 0 then

set pv to v

else

set c's value to pv

end if

end repeat

end tell

end tell

end tell

end tell

end tell





Result:


User uploaded file



When running the script be sure not to have both Numbers 2 and Numbers 3 running at the same time.


SG



Addendum: Haven't tested in Numbers 2 what happens if you have 0's in cells. AppleScript in Numbers 2 treats a blank as 0 rather than "" or missing value, an anomaly corrected in Numbers 3.

Mar 22, 2015 4:47 PM in response to SGIII

Again SGIII, more than I expected. Thank you.


A zero is treated like an empty cell. It is overwritten with the previous non zero valued cell.


Lastly, so that I understand what you provided, I inserted comments for each line of code. Would you please look over my comments to confirm that I get it?


My problem with learning AppleScript is that while I think I understand the code while reading it, I don't know the AppleScript vocabulary to create the code myself.


-DaverDee


Tells…

—From the selected range, in a table, on a sheet, in a document, within Numbers...



— Set the variable ‘pv’ to null.

set pv to ""


— Begin a loop starting from the initial cell in the selected range.

— Define ’c’ as value in the current cell.

repeat with c in cells


— Copy the value of the current cell to the variable ‘v’.

set v to c's value


— If the current cell value stored in the variable 'v' is not zero, then copy the value 'v' to the variable 'pv'.

ifvisnot 0 thensetpvtov


— Otherwise, set the cell value defined by 'c' to be the value stored in 'pv'.

--Note: If nothing has been stored in 'pv', as in the case when 'v' is zero which occurs if zero or null is the first cell value in the selected range, then the cell is populated with a blank.

else set c's value to pv


—Close the If.

end if


—Close the repeat loop.

end repeat

Mar 22, 2015 5:12 PM in response to DaverDee

Excellent annotation of what I was doing....


On this:



— If the current cell value stored in the variable 'v' is not zero, then copy the value 'v' to the variable 'pv'.

ifvisnot 0 thensetpvtov



This might be a little better:

– If the variable 'v' is not 0 (if the Numbers 2 cell is not blank) then copy the value in 'v' to the variable 'pv'


I used 'pv' as shorthand for 'previous value' or value in the previous cell.


AppleScript is relatively easy to read and tinker with. But as you have discovered there are all kinds of "gotchas" when trying to put together something from scratch that actually does what you want it to do. For example, getting this one to work on both versions of Numbers took a bit of fiddling about in Script Editor to see what works and what doesn't.


SG

Mar 23, 2015 5:37 AM in response to DaverDee

Hello


If you wish, you can do it manually as follows in a similar way explained in the linked article on Excel.


E.g., given source table as Table 1, set formula in A3 to =A2 and select A3 and copy it to clipboard:


User uploaded file



and apply filter to show only rows with blank cell in column A and select target rows:


User uploaded file



and paste from clipboard and unapply filter:


User uploaded file



which should result in what you want. (Copy column A and paste values on column A if necessary.)




And just in case, here's a script written for Numbers v2, which can distinguish empty cell from cell with number 0 or formula ="".


In order to use it, select the target range (which may be across multiple columns) and run the script.



_main() on _main() script o property pp : {} on _undo(ctx) tell ctx to activate tell application "System Events" to keystroke "z" using {command down} do shell script "sleep 0.2" end _undo tell application "Numbers" -- v2 tell document 1 tell (sheet 1 whose tables's selection range's class contains range) tell (table 1 whose selection range's class is range) if not (exists) then return tell selection range set {i1, i2} to {row 1's address, row -1's address} set format to text -- set format to text [1] set pp to columns's cells i1 thru i2's value my _undo(it) -- undo format change repeat with j from 1 to count my pp set t to {} repeat with i from i1 to i2 tell column j's cell i if my pp's item j's item (i - i1 + 1) = 0.0 then -- empty if t ≠ {} then set value to t else set t to value end if end tell end repeat end repeat end tell end tell end tell end tell end tell (* [1] An empty cell has value 0.0 even if it is formatted as text in Numbers v2. Using this behaviour, empty cell can be distinguished from cell with number 0 or formula ="". *) end script run script o end _main



E.g.,


User uploaded file



Script is tested with Numbers v2.0.5 under OS X 10.6.8.


Hope this may help,

H

Feb 15, 2016 3:15 PM in response to seanlamonby

seanlamonby wrote:


Hello SGIII,



Im wanting to use Applescript to locate blank cells in Numbers and fill them in with a "."




Hi Sean,


This script will work with Numbers 3.


tell application "Numbers"

tell front document to tell active sheet

tell (first table whose selection range's class is range)

set selRng to selection range

tell selRng to repeat with c in cells

tell c to if value is missing value then set value to "."

end repeat

end tell

end tell

end tell


As with the scripts above you select cells in Numbers before running the script. It will replace blanks in the selection with .


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.

Quickly fill in blank cells (Excel equivalent)?

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