Delete just one cell

I have a spreadsheet that looks like this:


I would like to be able to delete just one cell so everything jumps up, like so:


This is just a simplified example. The actual spreadsheet has more complex words and is over 6000 lines long.


Is there anyway I can do this? I know that Numbers doesn't have a delete cell command, but I was hoping that maybe someone has an AppleScript work around?

Posted on Aug 31, 2020 4:33 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 31, 2020 5:28 PM

ok, I am just a beginner with AppleScript, and I know this is NOT the fastest or most efficient method, but it seems to work.


but as I teach new students in VBA (that I do know very well), it aint wrong if it works... it just might not be the most efficient, but it isn't wrong.


let me know if this works for your setup, I would like it if it did. :)

Jason


tell application "Numbers"
-- the first three tells here get to the selected range, then use the first cell of that range
-- we are assuming you are using a single cell to delete here, like the OP
	tell front document's active sheet
		tell (first table whose selection range's class is range)
			tell selection range
                -- get the row and column numbers of that first cell in the selection
				set DelRow to the address of the row of the first cell
				set DelCol to the address of the column of the first cell
			end tell
            -- IF your selection is NOT the last row of the table, continue
			if DelRow < address of the row of the last cell of column DelCol then
                -- Remember the origianl cells location for later
				set firstcell to the name of cell DelCol of row (DelRow)
                -- Get the number of the lastrow of the column
				set lastrow to the address of the row of the last cell of column DelCol
				
                -- this is the part that might take a little while
                -- walk from the starting cell, and set its value to the value of the
                -- cell beneath it.
				repeat with i from DelRow to lastrow - 1
					set value of cell DelCol of row i to value of cell DelCol of row (i + 1)
				end repeat
                -- now set the value of the last cell in the column to nothing/MT
				set value of cell DelCol of row lastrow to ""
                -- I felt is useful to reselct the original cell again at the end
				set selection range to range firstcell
			else
                -- only show this message if there is nothing below to bring up
				display alert "Last cell of column Selected"
			end if
		end tell -- end tell of table
	end tell -- end tell of document1 front sheet
end tell -- end tell numbers

9 replies
Question marked as Top-ranking reply

Aug 31, 2020 5:28 PM in response to big_smile

ok, I am just a beginner with AppleScript, and I know this is NOT the fastest or most efficient method, but it seems to work.


but as I teach new students in VBA (that I do know very well), it aint wrong if it works... it just might not be the most efficient, but it isn't wrong.


let me know if this works for your setup, I would like it if it did. :)

Jason


tell application "Numbers"
-- the first three tells here get to the selected range, then use the first cell of that range
-- we are assuming you are using a single cell to delete here, like the OP
	tell front document's active sheet
		tell (first table whose selection range's class is range)
			tell selection range
                -- get the row and column numbers of that first cell in the selection
				set DelRow to the address of the row of the first cell
				set DelCol to the address of the column of the first cell
			end tell
            -- IF your selection is NOT the last row of the table, continue
			if DelRow < address of the row of the last cell of column DelCol then
                -- Remember the origianl cells location for later
				set firstcell to the name of cell DelCol of row (DelRow)
                -- Get the number of the lastrow of the column
				set lastrow to the address of the row of the last cell of column DelCol
				
                -- this is the part that might take a little while
                -- walk from the starting cell, and set its value to the value of the
                -- cell beneath it.
				repeat with i from DelRow to lastrow - 1
					set value of cell DelCol of row i to value of cell DelCol of row (i + 1)
				end repeat
                -- now set the value of the last cell in the column to nothing/MT
				set value of cell DelCol of row lastrow to ""
                -- I felt is useful to reselct the original cell again at the end
				set selection range to range firstcell
			else
                -- only show this message if there is nothing below to bring up
				display alert "Last cell of column Selected"
			end if
		end tell -- end tell of table
	end tell -- end tell of document1 front sheet
end tell -- end tell numbers

Aug 31, 2020 6:18 PM in response to jaxjason

this version seems to work really faster than the other option....


tell application "Numbers"
	activate -- you need to activate the app to use copy/paste below
	tell front document's active sheet
		tell (first table whose selection range's class is range)
			tell selection range
				set DelRow to the address of the row of the first cell
				set DelCol to the address of the column of the first cell
			end tell
			if DelRow < address of the row of the last cell of column DelCol then
				-- first cell to copy
				set firstcell to the name of cell DelCol of row (DelRow + 1)
				-- original cell name
				set OriginalCell to the name of the cell DelCol of row DelRow
				-- get last row of the column, last row to copy
				set lastrow to the address of the row of the last cell of column DelCol
				-- get the address of the last cell of that column
				set lastcell to the name of cell DelCol of row (lastrow)
				
				-- Set the selection to the range one down from the selected cell to teh bottom row
				set selection range to range (firstcell & ":" & lastcell)
				-- use keystroke emulation to send copy
				tell application "System Events" to keystroke "c" using command down
				delay 0.5 -- this seemed to be necesary to slow things down just a little bit
				set selection range to range OriginalCell --now select the original cell
				delay 0.5
				-- and use keyboard to paste the values in
				tell application "System Events" to keystroke "v" using command down
				delay 0.5
				-- now select the last cell at the bottom of the column
				set selection range to range (lastcell)
				--set it to nothing
				set value of first cell of the selection range to ""
			else
				display alert "Last cell of column Selected"
			end if
		end tell
	end tell
end tell

Sep 2, 2020 10:12 AM in response to big_smile

Since you like the power of AppleScript (good to see the interest!) here is another style of script for your reference. I think it will produce similar results to Jason's nice first script.


tell application "Numbers"
	tell front document to tell active sheet
		tell (first table whose class of selection range is range)
			tell first cell of selection range
				set rowNum to address of its row
				set colNum to address of its column
			end tell
			tell column colNum
				repeat with i from rowNum to (cell count)
					try
						set value of cell i to value of cell (i + 1)
					on error -- last row
						set value of cell i to missing value -- "blank" cell
					end try
				end repeat
			end tell
			set selection range to cell rowNum of column colNum -- return cursor to original cell
		end tell
	end tell
end tell


Do highly recommend becoming proficient at the drag operations Badunit describes, though. In most situations I find them more convenient than the Excel approach.


SG

Aug 31, 2020 8:07 AM in response to big_smile

There is no "delete one cell" command like in Excel (the selection where it compresses the row/column to delete the cells), But you can do it with your mouse. This ability to move cells, rows, and columns around in Numbers is really slick.


  1. Select all the cells you want to move (all those in column B below "Amy"). For a long table, the easiest way might be to click on "Becky" then Shift-Cmd-Down Arrow to get all the rest of the column.
  2. Click and hold on Becky then start dragging the selected cells upward one row and drop them. You could move them anywhere you like, actually.


You can move entire columns/rows around if you click then click-hold on a column letter or row number (or a range of them) and drag the column/row to a new location in the table, into a different table, or onto the canvas to create a new table from it.


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.

Delete just one cell

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