I’m new to JavaScript and learning it for Google Sheets. I’m working on a project to create reports using scripts because too many formulas were slowing down the sheet.
Here’s the issue: I have a sheet with form responses and need to generate reports from them, specifically for an Hourly Break Down sheet that currently supports only one report out of four. You select a team name in cell A28 and a date in cell I33, and the report is generated based on those values. The other reports cover data from the last 31 days.
I was considering copying my code four times and tweaking each one, but I’m thinking there might be a more efficient method using just one script and a single loop through the data. I also experimented with one script for the top half and another for the bottom half of the report.
Below is an example of my current code:
function generateHourlyReport() {
const DATA_SHEET = 'Form Responses';
const TARGET_SHEET = 'Hourly Break Down';
const TEAM_CELL = 'A28';
const DATE_CELL = 'I33';
const ss = SpreadsheetApp.getActiveSpreadsheet();
const dataSheet = ss.getSheetByName(DATA_SHEET);
const targetSheet = ss.getSheetByName(TARGET_SHEET);
const reportDate = new Date(targetSheet.getRange(DATE_CELL).getValue());
const teamName = targetSheet.getRange(TEAM_CELL).getValue();
const data = dataSheet.getDataRange().getValues();
const hourlyData = processData(data, reportDate, teamName);
writeReport(targetSheet, hourlyData);
}
function processData(data, reportDate, teamName) {
// Process data and return hourly breakdown
}
function writeReport(sheet, hourlyData) {
// Write the processed data to the report sheet
}
I’m open to any suggestions on how to improve efficiency. Thanks!