applescript to remove column containing at least one merged cell

While it is possible to delete manually a column from a Numbers table whether or not  it contains a merged cell,  AppleScript seems to be unable to remove a column containing merged cells.


Have I missed something?  Please could some knowledgeable person  advise  before I start looking for workarounds. 



MacBook Pro 15″, OS X 10.11

Posted on Sep 6, 2022 10:37 PM

Reply
Question marked as Top-ranking reply

Posted on Sep 7, 2022 8:36 AM

Starting with this table, where column A has two merged cells:




I ran this AppleScript (click in the table first and run):



tell application "Numbers"
	tell front document
		tell active sheet
			tell (first table whose class of selection range is range)
				delete its column "A"
			end tell
		end tell
	end tell
end tell



The result:




So the script not only removed column A but column B as well.


It's hard to see what is happening on your end without seeing the table and script.


In general I observe the following rule: Don't merge cells unless you absolutely need them!


There are usually other ways to produce the same visual effect.


SG

3 replies
Question marked as Top-ranking reply

Sep 7, 2022 8:36 AM in response to jbh2

Starting with this table, where column A has two merged cells:




I ran this AppleScript (click in the table first and run):



tell application "Numbers"
	tell front document
		tell active sheet
			tell (first table whose class of selection range is range)
				delete its column "A"
			end tell
		end tell
	end tell
end tell



The result:




So the script not only removed column A but column B as well.


It's hard to see what is happening on your end without seeing the table and script.


In general I observe the following rule: Don't merge cells unless you absolutely need them!


There are usually other ways to produce the same visual effect.


SG

Sep 7, 2022 2:56 PM in response to SGIII

With an unmerge command I can get your test script to not delete column B but it will unmerge any merged cells in column B (and in any other column that has a row merged with A).


I suppose it is related to what happens if you manually try to select a range of cells in column A. As soon as you get to a merged row the selection range jumps over to include the other column(s).


tell application "Numbers"
	tell front document
		tell active sheet
			tell (first table whose class of selection range is range)
				unmerge column "A"
				delete column "A"
			end tell
		end tell
	end tell
end tell

Sep 8, 2022 6:57 AM in response to SGIII

That was very helpful. Thank you. Made me realise what was going on in my table. (I say “my table” but I didn’t create it.  Like you I try to avoid merged cells.)  


In my table all of the cells in at least one row  are merged. AppleScript’s remove  tries to delete all the columns affected by the merge  as in your example. But  it  fails to do so presumably because it would have to delete all the columns in the table.


Manually deleting a column deletes only the targeted column, even when the targeted column includes cells merged row-wise.  (I see there is a similar difference  in behaviour when adding columns: - telling AppleScript to make one new column before column “A” doubles the number of columns in  my table, whereas the manual “Add Column Before” simply adds one column.)


So I need to unmerge all the cells in the table before removing the unwanted columns.  Unfortunately most of the rows in which all the cells were merged contained long spiels of text. That brings up another problem.  The table’s default text setting was “Wrap text in cell”, and unmerging results in each long spiel being squashed into the first cell of its row, the height of which is increased to accommodate the wrapped text. Changing the wrap setting doesn’t help because the text runs out of the table at right.


Finally, to make the table readable, I  re-merge the cells in the rows that were originally merged.  FWIW this is what I have done so far. For this test I want to delete columns D and E







tell application "Numbers" to tell front document to tell active sheet
	tell table 1
		--find rows with merged cells
		set mergedrows to {}
		set testcol to "C"
		set nrows to row count
		tell column testcol
			repeat with jrow from 1 to nrows
				set x to name of cell jrow
				set k to first character of x
				if k is not testcol then
					set mergedrows to mergedrows & jrow
				end if
			end repeat
		end tell
		set the selection range to the cell range
		
		unmerge selection range
		
		--delete columns as required
		set the selection range to range "D:E"
		delete selection range
		
		--make table readable again
		repeat with j in mergedrows
			merge row j
		end repeat
	end tell
	
end tell


Thanks again for giving me some clarity.



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.

applescript to remove column containing at least one merged cell

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