Merge cells w/notes - extra delimiters


#1

I’m working on a script that merges selected rows w/their notes. Everything is working properly with the exception of one issue.
When there is no ‘note’, it produces a notes field with additional ‘return’ characters in it. It makes sense why it’s doing it, I just don’t know how to make it stop.

Here’s the script:


tell front document of application "OmniOutliner"

set SibCount to (count of preceding sibling of first selected row)
set thecount to (count of selected rows)
set row_start to (index of first selected row) --skip non-selected rows
set row_end to (index of last selected row) --record last row for deletion	

if thecount < 2 then
	display dialog "Select rows to merge." buttons "Cancel" default button "Cancel"
end if

repeat with n from 1 to (thecount) --# of rows selected
	
	set row_one to text of cells of first selected row --make list from first row
	set row_c to 0 --change to 0 to include notes
	
	repeat with i from 1 to count row_one --# of items (columns) in first row
		
		set row_c to row_c + 1
		
		if n > 1 then
			set row_copy to text of cells of selected row (n + (row_start - SibCount) - 1) --make list from next row
			
			set {l1, l2} to {row_one, row_copy} --merge list items
			set newlist to {}
			repeat with i from 1 to (count l1)
				set end of newlist to ((item i of l1) as text) & return & item i of l2
			end repeat
			
			set value of cell row_c of first selected row to item row_c of newlist --insert merged items
			
			if row_c is (count row_one) then exit repeat --stop at last column
		end if
		
	end repeat
end repeat

repeat while thecount is greater than or equal to 2
	set theList to every selected row
	delete row id (id of (item thecount of (theList)))
	set thecount to (count of selected rows)
end repeat

end tell

So again, the result looks great, but if the merged cells have empty ‘note’ fields, it produces extra, unnecessary ‘return’ characters in the ‘notes’ field:

Any help would be GREATLY appreciated.


#2

@eiff

Your script throws off an error here. Can you give more specifics on what you are trying to get it to do?

SG


#3

Oh strange!

What the script does (at least for me), is it merges selected rows into one row.

So if there were three rows:

  • Row 1
  • Row 2
  • Row 3

I’d select the 3 rows, run the script, and it would merge the three rows into into a single row:

  • Row 1
    Row 2
    Row 3

It also merges the notes fields:

  • Row 1
    (Note 1)
  • Row 2
    (Note 2)
  • Row 3
    (Note 3)

Into 1 row with all notes:

  • Row 1
    Row 2
    Row 3
    (Note 1
    Note 2
    Note 3)

But when the notes field is blank, it has empty lines in the notes field of the resulting row. (See screenshot). That’s not what I want, if there is no note, I wouldn’t want it to result in empty lines in the notes field.

Hope that’s clear.


#4

@eiff

If, as in your illustration, you’re just merging titles and notes (i.e. you don’t need to merge other columns) then how about this shorter script? Recommend testing on a duplicate document and/or removing the last repeat loop to make sure it does what you want.

SG

tell front document of application "OmniOutliner"
	
	set {selRows, selRowCount} to {selected rows, count selected rows}
	if selRowCount < 2 then return -- do nothing if 2 or more rows aren't selected
	set {mergedTopics, mergedNotes} to {"", ""}
	
	repeat with r from 1 to selRowCount
		tell selRows's item r
			set mergedTopics to mergedTopics & topic & return
			if note ≠ "" then set mergedNotes to mergedNotes & note & return
		end tell
	end repeat
	
	set mergedTopics to mergedTopics's text 1 thru -2 -- remove trailing return
	set mergedNotes to mergedNotes's text 1 thru -2 -- remove trailing return
	tell selRows's item 1 to set {topic, note} to {mergedTopics, mergedNotes}
	
	-- deletes merged rows -- remove this repeat loop if prefer to delete manually
	repeat with r from selRowCount to 2 by -1 -- work up from the bottom
		delete selRows's item r
	end repeat
	
end tell

#5

Thanks so much for your help on this!

Seems like the script works fine when there’s ‘notes’ in the rows, but gives an error when there’s no ‘notes’ in the selected rows.

See 10 second video: https://v.usetapes.com/S5Ap7RxYV9


#6

@eiff

Helpful video! This version addressed the case where there are no notes.

SG

tell front document of application "OmniOutliner"
	
	set {selRows, selRowCount} to {selected rows, count selected rows}
	if selRowCount < 2 then return -- do nothing if 2 or more rows aren't selected
	set {mergedTopics, mergedNotes} to {"", ""}
	
	repeat with r from 1 to selRowCount
		tell selRows's item r
			set mergedTopics to mergedTopics & topic & return
			if note ≠ "" then set mergedNotes to mergedNotes & note & return
		end tell
	end repeat
	
	set mergedTopics to mergedTopics's text 1 thru -2 -- remove trailing return
	try -- ignore error if mergedNotes is empty
		set mergedNotes to mergedNotes's text 1 thru -2 -- remove trailing return
	end try

	tell selRows's item 1 to set {topic, note} to {mergedTopics, mergedNotes}
	
	-- deletes merged rows -- remove this repeat loop if prefer to delete manually
	repeat with r from selRowCount to 2 by -1 -- work up from the bottom
		delete selRows's item r
	end repeat
	
end tell

#7

YES!!! This is PERFECT.
Thanks so much SG, this is a wonderful help.


#8

@eiff

Glad it’s working there. Now that it’s doing the right thing, I’m going to use it here too.

Just in case you aren’t aware, if you want to merge two sibling rows that do not have notes you can do that without a script: move the cursor to the beginning of the second row and hit control-delete.

SG


#9

Thanks so much for this. The more easily available ‘merge selected rows’ puts all of the text into a single line. I was looking for one that would merge rows but preserve line breaks. This does it perfectly!