0% found this document useful (0 votes)
3 views

excel script

The function formatDailyWork creates or updates a monthly sheet in Google Sheets based on form responses. It formats the sheet with custom headers, styles, conditional formatting for the 'Status' column, and applies filters for data management. Additionally, it adjusts column widths and formats the 'Start Date' column to a specific date format.

Uploaded by

Raji Mol
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

excel script

The function formatDailyWork creates or updates a monthly sheet in Google Sheets based on form responses. It formats the sheet with custom headers, styles, conditional formatting for the 'Status' column, and applies filters for data management. Additionally, it adjusts column widths and formats the 'Start Date' column to a specific date format.

Uploaded by

Raji Mol
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

function formatDailyWork() {

const ss = SpreadsheetApp.getActiveSpreadsheet();
const formSheet = ss.getSheetByName("Form Responses 1");

// Get the current month and year for sheet naming


const currentDate = new Date();
const month = currentDate.toLocaleString('default', { month: 'long' }); // Full
month name (e.g., January)
const year = currentDate.getFullYear(); // Current year (e.g., 2025)
const sheetName = `${month} ${year}`; // e.g., "January 2025"

// Check if the sheet for the current month exists


let formattedSheet = ss.getSheetByName(sheetName);

if (!formattedSheet) {
// If the sheet doesn't exist, create it
formattedSheet = ss.insertSheet(sheetName);
} else {
// If it already exists, clear the existing data
formattedSheet.clear();
}

// Get form data


const responses = formSheet.getDataRange().getValues();
const headers = ["Name", "Start Date", "Project", "Task", "Status",
"Comments"]; // Custom headers (ignoring timestamp)
const data = responses.slice(1).map(row => row.slice(1)); // Skip the first
column (timestamp)

// Add headers to the new sheet


formattedSheet.appendRow(headers);

// Style headers
const headerRange = formattedSheet.getRange(1, 1, 1, headers.length);
headerRange.setFontWeight("bold")
.setFontSize(12)
.setHorizontalAlignment("center")
.setVerticalAlignment("middle")
.setBackground("#4CAF50") // Green header background
.setFontColor("#FFFFFF"); // White font color

// Add data rows to the new sheet


data.forEach((row) => {
formattedSheet.appendRow(row);
});

// Style data rows


const lastRow = formattedSheet.getLastRow();
const dataRange = formattedSheet.getRange(2, 1, lastRow - 1, headers.length);
dataRange.setFontSize(11)
.setHorizontalAlignment("left")
.setVerticalAlignment("middle");

// Set alternating row colors


const alternatingRange = formattedSheet.getRange(2, 1, lastRow - 1,
headers.length);
const rule = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied(`=ISEVEN(ROW())`)
.setBackground("#f9f9f9") // Light gray for even rows
.setRanges([alternatingRange])
.build();
formattedSheet.setConditionalFormatRules([rule]);

// Apply Conditional Formatting for "Status" column


const statusColumn = headers.indexOf("Status") + 1;
const statusRange = formattedSheet.getRange(2, statusColumn, lastRow - 1);

const statusRules = [
SpreadsheetApp.newConditionalFormatRule()
.whenTextContains("Completed")
.setBackground("#b6d7a8") // Light green for Completed
.setFontColor("#000000")
.setRanges([statusRange])
.build(),
SpreadsheetApp.newConditionalFormatRule()
.whenTextContains("On Progress")
.setBackground("#ffe599") // Yellow for On Progress
.setFontColor("#000000")
.setRanges([statusRange])
.build(),
];

formattedSheet.setConditionalFormatRules([...statusRules, rule]);

// Adjust column widths for neat display


formattedSheet.setColumnWidths(1, 1, 120); // Name
formattedSheet.setColumnWidths(2, 1, 110); // Start Date
formattedSheet.setColumnWidths(3, 1, 140); // Project
formattedSheet.setColumnWidths(4, 1, 200); // Task
formattedSheet.setColumnWidths(5, 1, 120); // Status
formattedSheet.setColumnWidths(6, 1, 200); // Comments

// Check and remove existing filter if any


if (formattedSheet.getFilter()) {
formattedSheet.getFilter().remove();
}

// Add filters for easy sorting and filtering


formattedSheet.getRange(1, 1, 1, headers.length).createFilter();

// Apply dd-MM-yyyy format to Start Date column


const startDateColumn = headers.indexOf("Start Date") + 1;
const startDateRange = formattedSheet.getRange(2, startDateColumn, lastRow - 1);
startDateRange.setNumberFormat("dd-MM-yyyy");
}

You might also like