Expanding one row of cells into multiple cells

Trying to expand a single row of cells into multiple rows based on a quantity in one cell. My ultimate goal is to make one label from each row so I’m using a Source Sheet to expand the data entry sheet into a usable form. Not sure if that makes any sense so here’s an example. Top data set is the data entry sheet. Bottom data set is what I want the Source Sheet to look like.


Hope that makes sense. Thanks for the help.

User uploaded file

iPad 2

Posted on Nov 18, 2018 5:14 PM

Reply
Question marked as Top-ranking reply

Posted on Nov 19, 2018 7:54 AM

You can try the script below.


  1. Copy-paste the script into Script Editor (in Application > Utilities).
  2. Make sure Script Editor.app is listed and checked at System Preferences > Security & Privacy > Privacy > Accessibility.
  3. Click anywhere in the table that looks like your first table in your post. (Note this is a "table" not a "sheet".)
  4. Click the triangle 'run' button in Script Editor.
  5. Click anywhere on the canvas of the sheet.
  6. Command-v to paste.


The new table will then appear automatically on that sheet. (If you don't like the default Header Column you can set up a blank table without a Header Column, click a cell in that table, and command-v or Edit > Paste and Match Format.)


SG



tell application "Numbers"

tell front document's active sheet

tell (first table whose selection range's class is range)

set vv to rows'scells'sformatted value

end tell

end tell

end tell


set text item delimiters to tab

set pasteStr to (vv's item 1 as text) & return #header row

set numRows to vv'slength

repeat with r from 2 to numRows

set qty to vv's item r's item 2

repeat qty times

set vv's item r's item 2 to 1 #set Qty to 1

set pasteStr to (pasteStr & vv's item r as text) & return

end repeat

end repeat

set the clipboard topasteStr

pasteStr#optional to view in Result pane

5 replies
Question marked as Top-ranking reply

Nov 19, 2018 7:54 AM in response to Glazier1627

You can try the script below.


  1. Copy-paste the script into Script Editor (in Application > Utilities).
  2. Make sure Script Editor.app is listed and checked at System Preferences > Security & Privacy > Privacy > Accessibility.
  3. Click anywhere in the table that looks like your first table in your post. (Note this is a "table" not a "sheet".)
  4. Click the triangle 'run' button in Script Editor.
  5. Click anywhere on the canvas of the sheet.
  6. Command-v to paste.


The new table will then appear automatically on that sheet. (If you don't like the default Header Column you can set up a blank table without a Header Column, click a cell in that table, and command-v or Edit > Paste and Match Format.)


SG



tell application "Numbers"

tell front document's active sheet

tell (first table whose selection range's class is range)

set vv to rows'scells'sformatted value

end tell

end tell

end tell


set text item delimiters to tab

set pasteStr to (vv's item 1 as text) & return #header row

set numRows to vv'slength

repeat with r from 2 to numRows

set qty to vv's item r's item 2

repeat qty times

set vv's item r's item 2 to 1 #set Qty to 1

set pasteStr to (pasteStr & vv's item r as text) & return

end repeat

end repeat

set the clipboard topasteStr

pasteStr#optional to view in Result pane

Nov 19, 2018 1:11 AM in response to Glazier1627

Hi G',


Took a while before it made sense.


Essentially you want as many copies of each row as the value in that row of column B, with all data copied except the value in column B, which will be set to 1 on each row in the second table.


I don't immediately see a way to control this using a formula, but at first glance, it looks like a script solution would be comparatively simple.


The script would need to:

Get the number of rows of data in Table 1 to know when to quit.

Get the total of column B to determine how many rows would be filled in Table 2. (May not be necessary)

Set a counter (counter 2) to the number of data rows in Table 1

Enter a 'read' loop that would repeat as many times as the value in that counter to:

  • get the values from each column in row 2
  • Set a second counter to the value in column B of that row
  • Enter a 'print' loop that would repeat as many times as the value in the second counter to:
    • set column A to the value read from column a of Table 1
    • set column b to the minimum of the current value of counter 2 or 1
    • set the values in columns C, D and E to the values read from those columns of Table 1
    • Move the focus of this loop to the next row of Table 2
    • Decrement counter 2, and repeat.
  • Decrement counter 1 and repeat with the next row.


That would be the overall pattern, but I'm sure I've missed some of the details. There are some talented scripters who can take it from there.


Regards,

Barry

Nov 22, 2018 9:50 AM in response to SGIII

Thank you, SG and Barry. The script did the trick. A couple of quick notes:


1) Thanks for including the instructions on how to make this work. I didn't even know that scripts existed so I was completely dependent on them.


2) Instead of clicking anywhere in the table I had to click in the first cell in the Quantity column otherwise it would return an error.


3) I added a Date column before the Quantity column and that returned an error so I had to move Date after Quantity and that did it.


Thanks again. That puts the finishing touches on spreadsheet - for now.


Brian

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.

Expanding one row of cells into multiple cells

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