can you analyze this code " function cleanData() { // Get the active spreadsheet and…
- User
can you analyze this code " function cleanData() { // Get the active spreadsheet and sheets const ss = SpreadsheetApp.getActiveSpreadsheet(); const importSheet = ss.getSheetByName("OTC Import"); if (!importSheet) { throw new Error('Import sheet "OTC Import" not found.'); } const cleanedSheet = ss.getSheetByName("CleanedData") || ss.insertSheet("CleanedData");
// Clear any existing content in the cleaned sheet cleanedSheet.clearContents();
// Get data from the import sheet const data = importSheet.getDataRange().getValues();
// Headers for the cleaned sheet const resultData = [["Employee Name", "Employee Number", "Total Hours (Capped at 8 per day)", "Number of Days Worked"]];
// Create an object to store capped hours and count rows per employee const employeeData = {};
// Loop through the data, starting from row 2 to skip headers for (let i = 1; i < data.length; i++) { const [name, number, date, totalHours] = data[i]; if (typeof totalHours !== 'number') continue; // Skip rows with invalid hours
code// Ensure the hours do not exceed 8 per day const cappedHours = Math.min(totalHours, 8); // Initialize employee data if it doesn't exist if (!employeeData[name]) { employeeData[name] = { number, totalHours: 0, daysWorked: 0 }; } // Increase the count of days worked (or rows) for the employee employeeData[name].daysWorked += 1; // Accumulate capped hours employeeData[name].totalHours += cappedHours;}
// Prepare data to write to the new sheet for (const name in employeeData) { const { number, totalHours, daysWorked } = employeeData[name]; resultData.push([name, number, totalHours, daysWorked]); }
// Write all collected data to the new sheet at once cleanedSheet.getRange(1, 1, resultData.length, resultData[0].length).setValues(resultData); } "
- Assistant
- User
can you also get the number of hours on the column F, which is the overtime hours, and add it on the regular work hours but put a separate column on for the overtime hours.
- Assistant
- User
also add another col for total hours + overtime hours
- Assistant
- User
can you analyze " function cleanData() { // Get the active spreadsheet and sheets const ss = SpreadsheetApp.getActiveSpreadsheet(); const importSheet = ss.getSheetByName("OTC Import"); if (!importSheet) { throw new Error('Import sheet "OTC Import" not found.'); } const cleanedSheet = ss.getSheetByName("CleanedData") || ss.insertSheet("CleanedData");
// Clear any existing content in the cleaned sheet cleanedSheet.clearContents();
// Get data from the import sheet const data = importSheet.getDataRange().getValues();
- Assistant
- User
can we base the hours using Total Hours with Overtime column instead of Total Hours (Capped at 8 per day)
- Assistant