I would like to split content of cells of one column (with;) to several columns
I have A;B;C inside one cell (numbers), I would like to have A in 1st column, B in 2nd column and C in 3rd column
iMac (27-inch Late 2009), iOS 7.1
I have A;B;C inside one cell (numbers), I would like to have A in 1st column, B in 2nd column and C in 3rd column
iMac (27-inch Late 2009), iOS 7.1
The best answer depends on the circumstances. If you need to do this all the time in your document, you might want to do it with a formula. If it is a one-time thing or seldom needed or you import data then convert to columns in "batch" mode" , a script might be better.
If you choose the script, I have posted one from SGIII below. Open the app Applescript Editor. Copy/Paste the script (starting with the word "try") into AppleScript Editor. Select the cell or range of cells you want to convert. Run the script from Applescript Editor (which will convert the cells and put it on the clipboard). Select a destination cell and Paste.
--spreads character separated values (comma, tab, or 😉 across columns in Numbers 3
--usage: select cells with text, run script, paste where wanted
--if dealing with ; or tab-separated change default below
--SGIII 2014.02 v1, incorporating the peerless csv-to-list handler by Nigel Garvey
--https://discussions.apple.com/thread/5477003?answerId=24778675022#24778675022
try --get the values from Numbers as delimited string
tell application "Numbers" to tell front document to tell active sheet
tell (first table whose class of selection range is range)
set mySelection to the selection range
tell mySelection
if address of last column of mySelection is greater than address ¬
of first column of my mySelection then error --only 1 column allowed
--read selected cells containing unsplit delimited values into a string
set csvString to ""
repeat with thisCell from 1 to (count of cells)
set isBlankCell to value of cell thisCell is missing value
if not isBlankCell then
set csvString to csvString & value of cellthisCell & return
else
set csvString to csvString & "" & return
end if
end repeat
end tell
end tell
end tell
if (count of csvString) < 1 then error -- no non-blank cells selected
set csvString to (characters 1 thru -2 of csvString) as text --remove trailing character
on error
display dialog "Did you select ONE column of cells with contents to split?" buttons {"Cancel"}
end try
--parse csv string, split into an AppleScript list, then convert list to tab-delimited string for pasting
set csvList to csvToList(csvString, {separator:";"}) --IF NEEDED REPLACE {} with {separator: tab} or {separator: ";"}
set tsvString to listToTSV(csvList)
set the clipboard totsvString
display notification "Click a cell and paste values" with title "Numbers"
--return tsvString -- uncomment this line to view output for debugging
to listToTSV(a_list)
set tsvString to ""
repeat with i from 1 to count of a_list
set tsvString to tsvString & delimit(itemi of a_list, tab) & return
end repeat
end listToTSV
to delimit(aLine, itemSeparator)
set oTID to AppleScript'stext item delimiters
set AppleScript'stext item delimiters to itemSeparator
set delimitedLine to aLine as string
set AppleScript'stext item delimiters to oTID
return delimitedLine
end delimit
on csvToList(csvText, implementation) (* Nigel Garvey http://macscripter.net/viewtopic.php?pid=125444#p125444 *)
set {separator:separator, trimming:trimming} to (implementation & {separator:",", trimming:false})
script o-- Lists for fast access.
property qdti : getTextItems(csvText, "\"")
property currentRecord : {}
property possibleFields : missing value
property recordList : {}
end script
set astid to AppleScript'stext item delimiters
set qdtiCount to (counto'sqdti)
set quoteInProgress to false
considering case
repeat with i from 1 to qdtiCount by 2 -- Parse odd-numbered items only.
set thisBit to item i of o's qdti
if ((count thisBit) > 0) or (i is qdtiCount) then
if (quoteInProgress) then
set AppleScript'stext item delimiters to "\""
set thisField to (items a thru (i - 1) of o's qdti) as string
set AppleScript'stext item delimiters to "\"\""
set thisField to thisField'stext items
set AppleScript'stext item delimiters to "\""
set end of o's currentRecord to thisField as string
set quoteInProgress to false
else if (i > 1) then
set end of o's currentRecord to item (i - 1) of o's qdti
end if
set o'spossibleFields to getTextItems(thisBit, separator)
set possibleFieldCount to (counto'spossibleFields)
repeat with j from 1 to possibleFieldCount
set thisField to itemj of o'spossibleFields
if ((count thisField each paragraph) > 1) then
set theseFields to thisField'sparagraphs
repeat with k from 1 to (count theseFields) - 1
set thisField to itemk of theseFields
if ((k > 1) or (j > 1) or (i is 1) or ((count trim(thisField, true)) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
set end of o'srecordList to o'scurrentRecord
set o's currentRecord to {}
end repeat
set thisField to end of theseFields
if ((j < possibleFieldCount) or ((countthisField) > 0)) then set end of o'scurrentRecord to trim(thisField, trimming)
else
if (((j > 1) and ((j < possibleFieldCount) or (i is qdtiCount))) or ((j is 1) and (i is 1)) or ((count trim(thisField, true)) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
end if
end repeat
else if (quoteInProgress) then
else if (i > 1) then
set a to i - 1
set quoteInProgress to true
end if
end repeat
end considering
if (o'scurrentRecord is not {}) then set end of o'srecordList to o'scurrentRecord
set AppleScript'stext item delimiters to astid
return o'srecordList
end csvToList
on getTextItems(txt, delim) --used by Nigel Garvey's csvToList
set astid to AppleScript'stext item delimiters
set AppleScript'stext item delimiters to delim
set tiCount to (counttxt'stext items)
set textItems to {}
repeat with i from 1 to tiCount by 4000
set j to i + 3999
if (j > tiCount) then set j to tiCount
set textItems to textItems & text items i thru j of txt
end repeat
set AppleScript'stext item delimiters to astid
return textItems
end getTextItems
on trim(txt, trimming) ----used by Nigel Garvey's csvToList
if (trimming) then
repeat with i from 1 to (count txt) - 1
if (txt begins with space) then
set txt to text 2 thru -1 of txt
else
exit repeat
end if
end repeat
repeat with i from 1 to (count txt) - 1
if (txt ends with space) then
set txt to text 1 thru -2 of txt
else
exit repeat
end if
end repeat
if (txt is space) then set txt to ""
end if
return txt
end trim
The best answer depends on the circumstances. If you need to do this all the time in your document, you might want to do it with a formula. If it is a one-time thing or seldom needed or you import data then convert to columns in "batch" mode" , a script might be better.
If you choose the script, I have posted one from SGIII below. Open the app Applescript Editor. Copy/Paste the script (starting with the word "try") into AppleScript Editor. Select the cell or range of cells you want to convert. Run the script from Applescript Editor (which will convert the cells and put it on the clipboard). Select a destination cell and Paste.
--spreads character separated values (comma, tab, or 😉 across columns in Numbers 3
--usage: select cells with text, run script, paste where wanted
--if dealing with ; or tab-separated change default below
--SGIII 2014.02 v1, incorporating the peerless csv-to-list handler by Nigel Garvey
--https://discussions.apple.com/thread/5477003?answerId=24778675022#24778675022
try --get the values from Numbers as delimited string
tell application "Numbers" to tell front document to tell active sheet
tell (first table whose class of selection range is range)
set mySelection to the selection range
tell mySelection
if address of last column of mySelection is greater than address ¬
of first column of my mySelection then error --only 1 column allowed
--read selected cells containing unsplit delimited values into a string
set csvString to ""
repeat with thisCell from 1 to (count of cells)
set isBlankCell to value of cell thisCell is missing value
if not isBlankCell then
set csvString to csvString & value of cellthisCell & return
else
set csvString to csvString & "" & return
end if
end repeat
end tell
end tell
end tell
if (count of csvString) < 1 then error -- no non-blank cells selected
set csvString to (characters 1 thru -2 of csvString) as text --remove trailing character
on error
display dialog "Did you select ONE column of cells with contents to split?" buttons {"Cancel"}
end try
--parse csv string, split into an AppleScript list, then convert list to tab-delimited string for pasting
set csvList to csvToList(csvString, {separator:";"}) --IF NEEDED REPLACE {} with {separator: tab} or {separator: ";"}
set tsvString to listToTSV(csvList)
set the clipboard totsvString
display notification "Click a cell and paste values" with title "Numbers"
--return tsvString -- uncomment this line to view output for debugging
to listToTSV(a_list)
set tsvString to ""
repeat with i from 1 to count of a_list
set tsvString to tsvString & delimit(itemi of a_list, tab) & return
end repeat
end listToTSV
to delimit(aLine, itemSeparator)
set oTID to AppleScript'stext item delimiters
set AppleScript'stext item delimiters to itemSeparator
set delimitedLine to aLine as string
set AppleScript'stext item delimiters to oTID
return delimitedLine
end delimit
on csvToList(csvText, implementation) (* Nigel Garvey http://macscripter.net/viewtopic.php?pid=125444#p125444 *)
set {separator:separator, trimming:trimming} to (implementation & {separator:",", trimming:false})
script o-- Lists for fast access.
property qdti : getTextItems(csvText, "\"")
property currentRecord : {}
property possibleFields : missing value
property recordList : {}
end script
set astid to AppleScript'stext item delimiters
set qdtiCount to (counto'sqdti)
set quoteInProgress to false
considering case
repeat with i from 1 to qdtiCount by 2 -- Parse odd-numbered items only.
set thisBit to item i of o's qdti
if ((count thisBit) > 0) or (i is qdtiCount) then
if (quoteInProgress) then
set AppleScript'stext item delimiters to "\""
set thisField to (items a thru (i - 1) of o's qdti) as string
set AppleScript'stext item delimiters to "\"\""
set thisField to thisField'stext items
set AppleScript'stext item delimiters to "\""
set end of o's currentRecord to thisField as string
set quoteInProgress to false
else if (i > 1) then
set end of o's currentRecord to item (i - 1) of o's qdti
end if
set o'spossibleFields to getTextItems(thisBit, separator)
set possibleFieldCount to (counto'spossibleFields)
repeat with j from 1 to possibleFieldCount
set thisField to itemj of o'spossibleFields
if ((count thisField each paragraph) > 1) then
set theseFields to thisField'sparagraphs
repeat with k from 1 to (count theseFields) - 1
set thisField to itemk of theseFields
if ((k > 1) or (j > 1) or (i is 1) or ((count trim(thisField, true)) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
set end of o'srecordList to o'scurrentRecord
set o's currentRecord to {}
end repeat
set thisField to end of theseFields
if ((j < possibleFieldCount) or ((countthisField) > 0)) then set end of o'scurrentRecord to trim(thisField, trimming)
else
if (((j > 1) and ((j < possibleFieldCount) or (i is qdtiCount))) or ((j is 1) and (i is 1)) or ((count trim(thisField, true)) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
end if
end repeat
else if (quoteInProgress) then
else if (i > 1) then
set a to i - 1
set quoteInProgress to true
end if
end repeat
end considering
if (o'scurrentRecord is not {}) then set end of o'srecordList to o'scurrentRecord
set AppleScript'stext item delimiters to astid
return o'srecordList
end csvToList
on getTextItems(txt, delim) --used by Nigel Garvey's csvToList
set astid to AppleScript'stext item delimiters
set AppleScript'stext item delimiters to delim
set tiCount to (counttxt'stext items)
set textItems to {}
repeat with i from 1 to tiCount by 4000
set j to i + 3999
if (j > tiCount) then set j to tiCount
set textItems to textItems & text items i thru j of txt
end repeat
set AppleScript'stext item delimiters to astid
return textItems
end getTextItems
on trim(txt, trimming) ----used by Nigel Garvey's csvToList
if (trimming) then
repeat with i from 1 to (count txt) - 1
if (txt begins with space) then
set txt to text 2 thru -1 of txt
else
exit repeat
end if
end repeat
repeat with i from 1 to (count txt) - 1
if (txt ends with space) then
set txt to text 1 thru -2 of txt
else
exit repeat
end if
end repeat
if (txt is space) then set txt to ""
end if
return txt
end trim
Thanks Badunit, I'm going to test solution.
I would like to split content of cells of one column (with;) to several columns