diff --git a/src/core/runtime.ts b/src/core/runtime.ts index 49b254f..ef8cf09 100644 --- a/src/core/runtime.ts +++ b/src/core/runtime.ts @@ -60,8 +60,20 @@ export function exposeGasGlobals(handlers: Record an export function setupMenuFromPlugins(plugins: ReportPlugin[]): void { const ui = SpreadsheetApp.getUi(); const mainMenu = ui.createMenu('📋 Работа с отчетами'); + let separatorAdded = false; plugins.forEach(plugin => { + if (plugin.topLevel) { + if (!separatorAdded) { + mainMenu.addSeparator(); + separatorAdded = true; + } + plugin.menuItems.forEach(item => { + mainMenu.addItem(item.label, item.functionName); + }); + return; + } + const subMenu = ui.createMenu(plugin.menuTitle); plugin.menuItems.forEach(item => { subMenu.addItem(item.label, item.functionName); diff --git a/src/core/sheets/reportLifecycle.ts b/src/core/sheets/reportLifecycle.ts index d2d78cb..cd631ff 100644 --- a/src/core/sheets/reportLifecycle.ts +++ b/src/core/sheets/reportLifecycle.ts @@ -16,6 +16,31 @@ export function hideSheetsByPrefix( }); } +export function isHiddenReportSheet( + name: string, + hidden: boolean, + prefixes: readonly string[] +): boolean { + if (!hidden) { + return false; + } + + return prefixes.some(prefix => name.includes(prefix)); +} + +export function deleteHiddenReportSheets( + prefixes: readonly string[], + spreadsheet?: GoogleAppsScript.Spreadsheet.Spreadsheet +): number { + const ss = spreadsheet || SpreadsheetApp.getActiveSpreadsheet(); + const sheets = ss.getSheets().filter(sheet => + isHiddenReportSheet(sheet.getName(), sheet.isSheetHidden(), prefixes) + ); + + sheets.forEach(sheet => ss.deleteSheet(sheet)); + return sheets.length; +} + export function createTimestampedSheet( prefix: string, spreadsheet?: GoogleAppsScript.Spreadsheet.Spreadsheet diff --git a/src/departments/salesOriginal/profile.ts b/src/departments/salesOriginal/profile.ts index eee7f5c..c852062 100644 --- a/src/departments/salesOriginal/profile.ts +++ b/src/departments/salesOriginal/profile.ts @@ -12,6 +12,7 @@ import { workloadPlugin } from './workload'; import { currentTasksPlugin } from './currentTasks'; import { salesOriginalColumnWidths } from './columnWidths'; import { withAppendFromConfig, withMenuTitle } from './pluginUtils'; +import { deleteOldReportsPlugin } from '../../reports/deleteOldReports'; export const salesOriginalProfile: DepartmentProfile = { id: 'salesOriginal', @@ -38,5 +39,6 @@ export const salesOriginalProfile: DepartmentProfile = { withAppendFromConfig(newTemplatesPlugin, ['createNewTemplatesRepors']), '1️⃣1️⃣ 🆕 Новые шаблоны' ), + deleteOldReportsPlugin, ], }; diff --git a/src/departments/universal/profile.ts b/src/departments/universal/profile.ts index 980cdc5..4787a63 100644 --- a/src/departments/universal/profile.ts +++ b/src/departments/universal/profile.ts @@ -10,6 +10,7 @@ import { workloadPlugin } from '../../reports/workload'; import { currentTasksPlugin } from '../../reports/currentTasks'; import { effectivenessPlugin } from '../../reports/effectiveness'; import { newTemplatesPlugin } from '../../reports/newTemplates'; +import { deleteOldReportsPlugin } from '../../reports/deleteOldReports'; export const universalProfile: DepartmentProfile = { id: 'universal', @@ -25,5 +26,6 @@ export const universalProfile: DepartmentProfile = { currentTasksPlugin, effectivenessPlugin, newTemplatesPlugin, + deleteOldReportsPlugin, ], }; diff --git a/src/reports/deleteOldReports/index.ts b/src/reports/deleteOldReports/index.ts new file mode 100644 index 0000000..0bcb6c2 --- /dev/null +++ b/src/reports/deleteOldReports/index.ts @@ -0,0 +1,40 @@ +import { REPORT_PREFIXES } from '../../core/constants'; +import { deleteHiddenReportSheets, isHiddenReportSheet } from '../../core/sheets/reportLifecycle'; +import { askUser } from '../../core/ui'; +import type { ReportPlugin } from '../types'; + +export function deleteOldReports(): void { + const ui = SpreadsheetApp.getUi(); + const prefixes = Object.values(REPORT_PREFIXES); + const ss = SpreadsheetApp.getActiveSpreadsheet(); + const count = ss.getSheets().filter(sheet => + isHiddenReportSheet(sheet.getName(), sheet.isSheetHidden(), prefixes) + ).length; + + if (count === 0) { + ui.alert('Скрытых старых отчетов не найдено.'); + return; + } + + const confirmed = askUser( + `Удалить скрытые листы старых отчетов (${count})? Это действие нельзя отменить.` + ); + if (!confirmed) { + return; + } + + const deleted = deleteHiddenReportSheets(prefixes, ss); + ui.alert(`Удалено листов: ${deleted}.`); +} + +export const deleteOldReportsPlugin: ReportPlugin = { + id: 'deleteOldReports', + menuTitle: 'Удалить старые отчеты', + topLevel: true, + menuItems: [ + { label: 'Удалить старые отчеты', functionName: 'deleteOldReports' }, + ], + handlers: { + deleteOldReports, + }, +}; diff --git a/src/reports/types.ts b/src/reports/types.ts index 1ca7e86..8bc17d8 100644 --- a/src/reports/types.ts +++ b/src/reports/types.ts @@ -8,6 +8,8 @@ export interface ReportPlugin { menuTitle: string; menuItems: MenuItem[]; handlers: Record any>; + /** Пункт сразу в главном меню, без подменю отчёта. */ + topLevel?: boolean; requiredConfigKeys?: string[]; requiredScriptProperties?: string[]; htmlFiles?: string[]; diff --git a/tests/deleteOldReports.test.ts b/tests/deleteOldReports.test.ts new file mode 100644 index 0000000..770370d --- /dev/null +++ b/tests/deleteOldReports.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from 'vitest'; +import { REPORT_PREFIXES } from '../src/core/constants'; +import { isHiddenReportSheet } from '../src/core/sheets/reportLifecycle'; + +const prefixes = Object.values(REPORT_PREFIXES); + +describe('isHiddenReportSheet', () => { + it('выбирает скрытый лист, если имя содержит префикс отчёта', () => { + expect( + isHiddenReportSheet('Отчет по просроченным задачам 09.10.2026 12:00', true, prefixes) + ).toBe(true); + }); + + it('оставляет видимый лист с тем же префиксом', () => { + expect( + isHiddenReportSheet('Отчет по просроченным задачам 09.10.2026 13:00', false, prefixes) + ).toBe(false); + }); + + it('не трогает скрытый лист без префикса отчёта', () => { + expect(isHiddenReportSheet('Config', true, prefixes)).toBe(false); + }); +}); diff --git a/tests/salesOriginalProfile.test.ts b/tests/salesOriginalProfile.test.ts index 188d245..85b9a0b 100644 --- a/tests/salesOriginalProfile.test.ts +++ b/tests/salesOriginalProfile.test.ts @@ -17,6 +17,7 @@ describe('salesOriginal profile', () => { 'completed', 'effectiveness', 'newTemplates', + 'deleteOldReports', ]); }); diff --git a/tests/universalProfile.test.ts b/tests/universalProfile.test.ts index a6a06d1..e98f903 100644 --- a/tests/universalProfile.test.ts +++ b/tests/universalProfile.test.ts @@ -15,6 +15,7 @@ describe('universal profile', () => { 'currentTasks', 'effectiveness', 'newTemplates', + 'deleteOldReports', ]); });