Pages

joi, 20 noiembrie 2025

Google Apps Script : keep sheets whose names include today’s date ...

Today, this simple example will keep sheets whose names include today’s date in flexible regex formats
The script not include the month names (e.g., “20-Nov-2025”)
On running keeps sheets whose names contain any of these combinations; deletes the rest.
Let's see the source code:
function deleteSheetsWithoutToday() {
// Get today's components (zero-padded and variants)
var now = new Date();
var tz = Session.getScriptTimeZone();

var dd = Utilities.formatDate(now, tz, "dd"); // e.g., "20"
var d = Utilities.formatDate(now, tz, "d"); // e.g., "20" (no leading zero if <10)
var mm = Utilities.formatDate(now, tz, "MM"); // e.g., "11"
var m = Utilities.formatDate(now, tz, "M"); // e.g., "11" (no leading zero if <10)
var yyyy = Utilities.formatDate(now, tz, "yyyy"); // e.g., "2025"
var yy = Utilities.formatDate(now, tz, "yy"); // e.g., "25"

// Build a regex that matches many possible date embeddings in the sheet name
var dateRegex = buildFlexibleDateRegex({ dd: dd, d: d, mm: mm, m: m, yyyy: yyyy, yy: yy });

// Active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();

var kept = 0;
var deleted = 0;

sheets.forEach(function(sheet) {
var name = sheet.getName();

// If the name contains a combination of today's date components (any of the supported formats), keep it
if (dateRegex.test(name)) {
kept++;
Logger.log("KEEP: " + name);
} else {
ss.deleteSheet(sheet);
deleted++;
Logger.log("DELETE: " + name);
}
});

Logger.log("Summary → Kept: " + kept + ", Deleted: " + deleted);
}

/**
* Build a flexible regex that matches today's date in common formats.
* It covers:
* - dd[sep]mm[sep](yyyy|yy)
* - mm[sep]dd[sep](yyyy|yy)
* - (yyyy|yy)[sep]mm[sep]dd
* - (yyyy|yy)[sep]dd[sep]mm
* - contiguous forms like ddmmyyyy, mmddyyyy, yyyymmdd, ddmmyy, etc.
* - allows multiple separator types: -, _, ., /, space, colon
* - accepts zero-padded and non-padded day/month (e.g., "7" or "07")
*/
function buildFlexibleDateRegex(parts) {
var dd = parts.dd; // zero-padded day
var d = parts.d; // non-padded day
var mm = parts.mm; // zero-padded month
var m = parts.m; // non-padded month
var yyyy = parts.yyyy;
var yy = parts.yy;

// Separator class: one or more of -, _, ., /, space, or colon
var SEP = "[\\-_.\\/\\s:]+";

// Day and month alternatives (padded or not)
var DAY = "(?:" + dd + "|" + d + ")";
var MONTH = "(?:" + mm + "|" + m + ")";
var YEAR = "(?:" + yyyy + "|" + yy + ")";

// Ordered patterns with separators
var withSeps = [
DAY + SEP + MONTH + SEP + YEAR, // dd-mm-yyyy or dd/mm/yy, etc.
MONTH + SEP + DAY + SEP + YEAR, // mm-dd-yyyy
YEAR + SEP + MONTH + SEP + DAY, // yyyy-mm-dd
YEAR + SEP + DAY + SEP + MONTH // yyyy-dd-mm
];

// Contiguous patterns (no separators)
var noSeps = [
dd + mm + yyyy,
dd + mm + yy,
mm + dd + yyyy,
mm + dd + yy,
yyyy + mm + dd,
yy + mm + dd
];

// Optional surrounding non-digit boundaries to avoid matching inside longer numbers
// We’ll use word boundaries plus lookarounds to be more permissive with symbols.
var prefix = "(?<!\\d)"; // no digit before
var suffix = "(?!\\d)"; // no digit after

// Combine all patterns into a single alternation
var combined =
prefix +
"(?:" +
withSeps.join("|") +
"|" +
noSeps.join("|") +
")" +
suffix;

// Make the regex case-insensitive and global
return new RegExp(combined, "i");
}