Reputation: 125
I have a Google Sheet that I want to format. The sheet contains multiple rows with unique values.
I want to be able to format the data into this:
I have tried using query but I am only successful in getting the student id and am unable to for the rest of the data.
In my sample sheet, I filled out the formatted table without formula.
Here is the link to my sheet: https://docs.google.com/spreadsheets/d/1lebcSlt-k7-Ub71n7iGq9pZFmM9q4fHPLpw4pAvXAs4/edit?usp=sharing
Upvotes: 0
Views: 481
Reputation: 11214
To merge the cells, you will need a script. See the script below that also outputs the data and formatting:
function populateMasterSchedule() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var scheduleReport = spreadsheet.getSheetByName('Schedule Report');
var outputSheet = spreadsheet.getSheetByName('MASTER SCHEDULE');
// filter rows with student id starting from row 2
var dataRows = scheduleReport.getDataRange().getDisplayValues().slice(1).filter(row => row[3]);
var lastStudent, lastMerge = 0, startRow = 3, counter = 1, output = [];
dataRows.map((row, index) => {
// if current row's student id is not equal to previous or the last row
if((index != 0 && lastStudent != row[3]) || index == dataRows.length - 1) {
// need adjustments when it comes to the last iteration, add 1 row to merge
if (index == dataRows.length - 1)
index++;
// merge first 6 and the 8th columns vertically
outputSheet.getRange(lastMerge + startRow, 1, index - lastMerge, 6).mergeVertically();
outputSheet.getRange(lastMerge + startRow, 8, index - lastMerge, 1).mergeVertically();
lastMerge = index;
counter++;
}
lastStudent = row[3];
output.push([counter, row[3], row[1], row[2], '', row[9], '', '', row[7] + ', ' + row[8],
row[6], '', '', '', '', '', '', '', row[12]]);
});
var outputRange = outputSheet.getRange(startRow, 1, output.length, output[0].length);
outputRange.setValues(output);
outputRange.setBorder(true, true, true, true, true, true);
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Populate Master Schedule', 'populateMasterSchedule')
.addToUi();
}
Upvotes: 1