Is This Possible In Numbers?

I have Numbers at work but don’t have time to mess around with scripts. I’m thinking about purchasing Numbers to try what I need to do, but before I make the purchase, I want to see if it’s even possible to do this.

I’m a graphic designer at a newspaper. I’m the lucky one that gets to do our large two page Howard Hanna real estate spread with 140 listings with pictures, description, etc. Using InDesign’s data merge and a spreadsheet of their properties I’ve got things mostly automated. Now, I have two spreadsheets for the listings. One for all of their properties (around 900 listings), one for the properties running that week (around 140). Right now I manually search for each property running that week and manually copy it over. Kind of a hassle and not really very efficient.

What I would like to do is just type in the first column of the weekly listings the MLS number of each property and then have a script search the complete listings for that MLS number (also in the first column). If it finds it, copy that entire row in to the corresponding row in the weekly. If not, skip it. Is this possible?

In my research I’ve seen some things about comparing documents, but none of the threads I found really seemed to lead to what I want to do. I have really limited experience with AppleScript, but it does make sense to me and I have written a couple scripts, so I’m not completely in the dark, but I’d like to know if spending the $20 to experiment would even be worth it. Thanks.

I’m not an expert at scripting iWork Numbers, but I do know that what you want to do is most definitely possible. Someone more expert will undoubtedly weigh in. To do that effectively, however, they will probably need a sample of what a few rows of your master copy and weekly copies look like, including headings if that makes things clearer.

Send sample files to my mailbox
koenigyvansfrfr

Of course, replace by a period char (.)
and
replace by @

Yvan KOENIG (VALLAURIS, France) vendredi 19 août 2011 10:36:48

You may download for free the complete iWork package which will give every features during 30 days.
If you download it, apply immediately the available update.

Yvan KOENIG (VALLAURIS, France) vendredi 19 août 2011 10:39:24

Thanks for your responses.

I figured it’d be possible, but wanted some verification before continuing. I’m going to send some sample files to Yvan, but is there a service of choice for posting things in the forum?

Here is my script.

I know that some users dislike GUI scripting but with Numbers it’s the best way to keep every attributes/formats of datas.
As it extracts and insert the entire sets of datas in a single operation it’s the fastest responce.


--{code}
--[SCRIPT from_listings_to_weekly]
(*
Enregistrer le script en tant que Script : from_listings_to_weekly.scpt
déplacer le fichier ainsi créé dans le dossier
<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:Numbers:
Il vous faudra peut-être créer le dossier Numbers et peut-être même le dossier Applications.

Ouvrir les fichiers Listings.numbers et Weekly Listings.numbers 
Aller au menu Scripts , choisir Numbers puis choisir "from_listings_to_weekly"
Le script remplit les fiches du fichier Weekly Listings.numbers 

--=====

L'aide du Finder explique:
L'Utilitaire AppleScript permet d'activer le Menu des scripts :
Ouvrez l'Utilitaire AppleScript situé dans le dossier Applications/AppleScript.
Cochez la case "Afficher le menu des scripts dans la barre de menus".
Sous 10.6.x,
aller dans le panneau "Général" du dialogue Préférences de l'Éditeur Applescript
puis cocher la case "Afficher le menu des scripts dans la barre des menus".

--=====

Save the script as a Script: from_listings_to_weekly.scpt

Move the newly created file into the folder:
<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:Numbers:
Maybe you would have to create the folder Numbers and even the folder Applications by yourself.

Open the files Listings.numbers and Weekly Listings.numbers
Go to the Scripts Menu, choose Numbers, then choose "from_listings_to_weekly"
The script fill the records in the file Weekly Listings.numbers

--=====

The Finder's Help explains:
To make the Script menu appear:
Open the AppleScript utility located in Applications/AppleScript.
Select the "Show Script Menu in menu bar" checkbox.
Under 10.6.x,
go to the General panel of AppleScript Editor's Preferences dialog box
and check the "Show Script menu in menu bar" option.

--=====

Yvan KOENIG (VALLAURIS, France)
2011/08/20
*)
--=====

on run
	local document_source, document_cible, ouvert1, ouvert2, attention, extraits
	set document_source to "Listings.numbers"
	set document_cible to "Weekly Listings.numbers"
	(*
Check that the wanted documents are open *)
	tell application "Numbers"
		set ouvert1 to exists document document_source
		set ouvert2 to exists document document_cible
	end tell
	
	set attention to {}
	if my parleAnglais() then
		if not ouvert1 then copy "The document "" & document_source & "" is not open !" to end of attention
		if not ouvert2 then copy "The document "" & document_cible & "" is not open !" to end of attention
	else
		if not ouvert1 then copy "Le document « " & document_source & " » n'est pas ouvert !" to end of attention
		if not ouvert2 then copy "Le document « " & document_cible & " » n'est pas ouvert !" to end of attention
	end if
	
	if attention is not {} then error my recolle(attention, return)
	
	my activateGUIscripting()
	(*
Put document_source at front *)
	my selectMenu("Numbers", 10, document_source)
	(*
Select every cells of table 1 of sheet 1 starting from cell A2 *)
	tell application "Numbers" to tell document document_source to tell sheet 1 to tell table 1
		set selection range to range ("A2 : " & name of last cell)
	end tell
	(*
Copy the selected cells *)
	set source to my copyToClipboard("Numbers")
	(*
Put document_cible at front *)
	my selectMenu("Numbers", 10, document_cible)
	(*
Extract the list of MLS thru Copy to Clipboard to keep them as 'strings'
Extracting them the standard way 'get value' would coerce them in scientific format *)
	tell application "Numbers" to tell document document_cible to tell sheet 1 to tell table 1
		set selection range to range ("A2 : " & name of last cell of column 1)
	end tell
	set des_MLS to paragraphs of my copyToClipboard("Numbers")
	(*
Extract the datas linked to the MLSs 
Edited on 2011/08/22 *)
	set extraits to {}
	repeat with un_MLS in des_MLS
		try
			copy paragraph 1 of item 2 of my decoupe(source, return & un_MLS & tab) to end of extraits
		on error
			copy tab to end of extraits
		end try
	end repeat
	(*
Put these datas in the clipboard *)
	set the clipboard to my recolle(extraits, return)
	(*
Select the cell B2 which is the first cell to fill *)
	tell application "Numbers" to tell document document_cible to tell sheet 1 to tell table 1
		set selection range to range "B2"
	end tell
	(*
Paste matching style *)
	my raccourci("Numbers", "v", "cas")
end run

--=====

on parleAnglais()
	local z
	try
		tell application "Numbers" to set z to localized string "Cancel"
	on error
		set z to "Cancel"
	end try
	return (z is not "Annuler")
end parleAnglais

--=====

on decoupe(t, d)
	local oTIDs, l
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to d
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

--=====

on recolle(l, d)
	local oTIDs, t
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to d
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

--=====

on activateGUIscripting()
	(* to be sure than GUI scripting will be active *)
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true
	end tell
end activateGUIscripting

--=====
(*
==== Uses GUIscripting ==== 
*)
(*
This handler may be used to 'type' text, invisible characters if the third parameter is an empty string. 
It may be used to 'type' keyboard raccourcis if the third parameter describe the required modifier keys. 

I changed its name « shortcut » to « raccourci » to get rid of a name conflict in Smile. 
*)
on raccourci(a, t, d)
	local k
	tell application a to activate
	tell application "System Events" to tell application process a
		set frontmost to true
		try
			t * 1
			if d is "" then
				key code t
			else if d is "c" then
				key code t using {command down}
			else if d is "a" then
				key code t using {option down}
			else if d is "k" then
				key code t using {control down}
			else if d is "s" then
				key code t using {shift down}
			else if d is in {"ac", "ca"} then
				key code t using {command down, option down}
			else if d is in {"as", "sa"} then
				key code t using {shift down, option down}
			else if d is in {"sc", "cs"} then
				key code t using {command down, shift down}
			else if d is in {"kc", "ck"} then
				key code t using {command down, control down}
			else if d is in {"ks", "sk"} then
				key code t using {shift down, control down}
			else if (d contains "c") and (d contains "s") and d contains "k" then
				key code t using {command down, shift down, control down}
			else if (d contains "c") and (d contains "s") and d contains "a" then
				key code t using {command down, shift down, option down}
			end if
		on error
			repeat with k in t
				if d is "" then
					keystroke (k as text)
				else if d is "c" then
					keystroke (k as text) using {command down}
				else if d is "a" then
					keystroke k using {option down}
				else if d is "k" then
					keystroke (k as text) using {control down}
				else if d is "s" then
					keystroke k using {shift down}
				else if d is in {"ac", "ca"} then
					keystroke (k as text) using {command down, option down}
				else if d is in {"as", "sa"} then
					keystroke (k as text) using {shift down, option down}
				else if d is in {"sc", "cs"} then
					keystroke (k as text) using {command down, shift down}
				else if d is in {"kc", "ck"} then
					keystroke (k as text) using {command down, control down}
				else if d is in {"ks", "sk"} then
					keystroke (k as text) using {shift down, control down}
				else if (d contains "c") and (d contains "s") and d contains "k" then
					keystroke (k as text) using {command down, shift down, control down}
				else if (d contains "c") and (d contains "s") and d contains "a" then
					keystroke (k as text) using {command down, shift down, option down}
				end if
			end repeat
		end try
	end tell
end raccourci

--=====
(*
my selectMenu("Pages",5, 12)
==== Uses GUIscripting ====
*)
on selectMenu(theApp, mt, mi)
	tell application theApp
		activate
		tell application "System Events" to tell process theApp to tell menu bar 1 to ¬
			tell menu bar item mt to tell menu 1 to click menu item mi
	end tell -- application theApp
end selectMenu

--=====

on copyToClipboard(theApp)
	local contenu
	set the clipboard to ""
	(*
Copy the selection to the clipboard *)
	my raccourci(theApp, "c", "c") (* Copy *)
	(*
Wait for achievement of the copy process *)
	repeat
		try
			set contenu to the clipboard as text
			if contenu > "" then
				exit repeat
			else
				error number 12345
			end if
		on error
			delay 0.1
		end try
	end repeat
	return contenu
end copyToClipboard

--=====
--[/SCRIPT]
--{code}

Yvan KOENIG (VALLAURIS, France) samedi 20 août 2011 13:36:52

Okay. This script does exactly what I want it to do, but it only works on the sample files I sent to Yvan. When I try using it on the actual files I need to use it on it doesn’t do anything. If this is an issue of the files not being the same number of records as the samples I sent, then it won’t really work for me as the number of properties in the master list changes every week (houses sell, new listings, etc.). If I had to change something in the script every week, that would be better than doing all the work manually, but ideally this would be something that required no work on the script weekly because if I’m on vacation or sick, the other designers won’t know how to fix things.

If I run it from the scripts menu, I just have nothing copy over. If I run it from the editor I get an error message about not being able to get item 2 of… and then it basically looks like it just lists the entire rest of the document.

I tried looking it over myself, but the script wound up being more complicated than I thought it would be and I didn’t have any luck figuring it out. I was kind of envisioning it would be as simple as “Find X. If found, copy row containing X.” But I know the Applescript support in Numbers is not exactly great. Anyone have any thoughts?

Send true files to my mailbox.

I can’t guess the differences between the samples and the true ones.

What is sure is that it’s not a problem of number of records.

Yvan KOENIG (VALLAURIS, France) dimanche 21 août 2011 23:04:37

There weren’t any differences other than number of rows. I literally just opened my file, deleted a bunch of rows and then sent you the file. It’s not an issue of any differences anyway, I figured out the problem:

If a listing is missing from the master list (which will happen from time to time in my real usage) it skips everything instead of just the missing listing.

I edited my original message to take care of this case.

Yvan KOENIG (VALLAURIS, France) lundi 22 août 2011 11:31:52

Excellent. Thank you very much. I appreciate it a lot.

I’ll try it out at work tomorrow.