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 ss = SpreadsheetApp.getActiveSpreadsheet(); const inputSheet = ss.getSheetByName(inputSheetName); let outputSheet = ss.getSheetByName(outputSheetName);
// Create the output sheet if it doesn't exist if (!outputSheet) { outputSheet = ss.insertSheet(outputSheetName); } else { outputSheet.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 sheet outputSheet.appendRow(newHeaders);
let currentName = null; let groupData = []; let rowIndex = 2; // Start writing data from the second row (first row is headers)
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); } // 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); } }
function writeGroup(sheet, groupData, headers, regularHoursIndex, ot1HoursIndex, startRow) { const headersLength = headers.length;
// Write group rows with new columns calculated const processedData = groupData.map(row => { const paidHours = Math.min(row[regularHoursIndex], 8); // Cap at 8 const totalPaidOT1 = paidHours + (row[ot1HoursIndex] || 0);
codeconst newRow = [...row]; newRow.splice(regularHoursIndex + 1, 0, paidHours); newRow.splice(ot1HoursIndex + 2, 0, totalPaidOT1); return newRow; - Assistant
- User
please create another sheet, with just 1 name and then the total, and name the sheet as summary
- Assistant