analyze this code " function cleanOTCInput() { const inputSheetName = "OTC-Input"; //…
- User
analyze this code " function cleanOTCInput() { const inputSheetName = "OTC-Input"; // Name of the input sheet const outputSheetName = "CleanData"; // Name of the output sheet const summarySheetName = "SummaryData"; // Name of the summary sheet
const ss = SpreadsheetApp.getActiveSpreadsheet(); const inputSheet = ss.getSheetByName(inputSheetName); let outputSheet = ss.getSheetByName(outputSheetName); let summarySheet = ss.getSheetByName(summarySheetName);
// Create the output and summary sheets if they don't exist if (!outputSheet) { outputSheet = ss.insertSheet(outputSheetName); } else { outputSheet.clear(); // Clear existing content }
if (!summarySheet) { summarySheet = ss.insertSheet(summarySheetName); } else { summarySheet.clear(); // Clear existing content }
const data = inputSheet.getDataRange().getValues(); const headers = data[0];
// Columns to remove const removeColumns = [ "Overtime 2 Hours", "Overtime 3 Hours", "Vacation Hours", "Holiday Hours", "Sick Hours", "Personal Hours", "First Punch In", "Last Punch Out", "Tip Amount", "Bonus Amount", "Commission Amount", "Mileage" ];
// Filter out unwanted columns const keepIndexes = headers .map((header, index) => (removeColumns.includes(header) ? null : index)) .filter(index => index !== null);
// Keep only relevant data const filteredData = data.map(row => keepIndexes.map(index => row[index])); const filteredHeaders = keepIndexes.map(index => headers[index]);
// Add new columns: Paid Hours and Total Hours (Paid + Overtime 1) const newHeaders = [...filteredHeaders]; const regularHoursIndex = filteredHeaders.indexOf("Regular Hours"); const ot1HoursIndex = filteredHeaders.indexOf("Overtime 1 Hours");
newHeaders.splice(regularHoursIndex + 1, 0, "Paid Hours"); newHeaders.splice(ot1HoursIndex + 2, 0, "Total Paid + OT1 Hours");
// Write headers to the output and summary sheets outputSheet.appendRow(newHeaders); summarySheet.appendRow(newHeaders);
let currentName = null; let groupData = []; let rowIndex = 2; // Start writing data from the second row (first row is headers) let summaryData = []; // To collect total rows for the SummaryData sheet
for (let i = 1; i < filteredData.length; i++) { const row = filteredData[i]; if (row[0] !== currentName) { // Process the previous group if (groupData.length > 0) { rowIndex = writeGroup(outputSheet, groupData, newHeaders, regularHoursIndex, ot1HoursIndex, rowIndex); const totalsRow = calculateTotals(groupData, newHeaders, regularHoursIndex + 1, ot1HoursIndex + 2); summaryData.push(totalsRow); } // Start new group currentName = row[0]; groupData = []; } groupData.push(row); }
// Process the final group if (groupData.length > 0) { writeGroup(outputSheet, groupData, newHeaders, regularHoursIndex, ot1HoursIndex, rowIndex); const totalsRow = calculateTotals(groupData, newHeaders, regularHoursIndex + 1, ot1HoursIndex + 2); summaryData.push(totalsRow); }
- Assistant
- User
can you re-create the code, with minor changes, because on the 'SummaryData' sheet, it doesn't display the total on the column F G H
- Assistant
- User
it still does'nt show the total
- Assistant
- User
lets remove the summary generation sheet.
- Assistant