Reputation: 43
I have done a bit of googling/ research and the code below is the closest to what I wanted to accomplish.
I have a spreadsheet with data and what the code does is to look at column A of the the source sheet and creates new sheets with the name in the that column and then carries all row data that matches that name to the new sheet.
This code works perfectly fine, I will appreciate help to modify it such that:
While copying matching values, It should include first row (A1, The title row)
function backUP() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getActiveSheet();
var sheet1 = ss.getSheetByName("Source")
var getNames = [...new Set(sheet1.getRange("A2:A").getValues().filter(String).toString().split(","))];
for (var i = 0; i < getNames.length; i++) {
var copy = ss.getSheetByName(getNames[i]);
if (copy) {
Logger.log("Sheet already exists");
} else {
var rowIndexes = sheet1.getRange("A:A").getValues()
.map((value, index) => [value[0], (index + 1)])
.filter(value => value[0] === getNames[i]);
var namedSheet = ss.insertSheet(getNames[i]);
rowIndexes.map(index => {
var rowValues = sheet1.getRange(index[1], 1, 1, sheet1.getLastColumn()).getValues();
namedSheet.appendRow(rowValues[0]);
});
ss.setActiveSheet(ss.getSheetByName(getNames[i]));
ss.moveActiveSheet(ss.getNumSheets());
}
}
}
Upvotes: 0
Views: 1640