Reputation: 13
I'm trying to run a script on Google Sheets that will allow me to copy a number of users a set amount of times and add them as a list.
A coworker made this code for me:
function BulkUpload() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getCurrentCell().offset(0, 0, 8, 1).activate();
spreadsheet.getActiveSheet().insertRowsBefore(spreadsheet.getActiveRange().getRow(), 8);
spreadsheet.getCurrentCell().offset(8, 0).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
spreadsheet.getCurrentCell().offset(9, 0).activate();
Since I needed this for 36 users, what I did is that I copied the script and pasted it 36 times. This worked great and I ended up with a list of my 36 users copied 9 times each, but there's two problems:
In order to fix these 2 issues, I would need:
As an example, let's say that we have 7 users and I wanna copy each 6 times. I'd like to be able to go to my Google Sheets, have this little table and add the numbers needed, (ie, 5 in cell B1 and 10 in cell B2 according to this table:
Column A | Column B |
---|---|
Repetitions needed | 6 |
Users | 7 |
Then, after this is specified, I would paste the list and run the script, and this should take me from:
to this: GoogleSheets Screenshot
For transparency, the numbers I have to do are quite large (hundreds of users and dozens of repetitions) so that's why I'd need the script, I just made it simple here so I could add an example without adding too much space.
Do note that both my coworker and I are complete beginners when it comes to coding so there might be much easier ways to accomplish this; I'm open to any other solution that achieves the same desired result.
Thank you!
Upvotes: 1
Views: 69
Reputation: 11184
Your script seems manual and I think this approach would be better. But in this one, you would need to list the names on the sheet itself, whether in a separate cell or as a parameter.
function BulkUpload(names, repetition) {
var output = Array(names.length * repetition);
names.split(",").forEach(function (name, index) {
output.fill([name.trim()], repetition * index, repetition * (index + 1));
});
return output;
}
=BulkUpload(B2, B1)
=BulkUpload("Alex, John, Sam, Bob, Tony, Laura, Victor", B1)
Upvotes: 1