Add deleteOldReports plugin to main menu and implement functionality to delete hidden report sheets. Enhance report lifecycle management with new utility functions for identifying and removing hidden sheets based on prefixes.
This commit is contained in:
parent
a0f673c2e8
commit
39eed702f1
|
|
@ -60,8 +60,20 @@ export function exposeGasGlobals(handlers: Record<string, (...args: any[]) => 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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
],
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
],
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
};
|
||||
|
|
@ -8,6 +8,8 @@ export interface ReportPlugin {
|
|||
menuTitle: string;
|
||||
menuItems: MenuItem[];
|
||||
handlers: Record<string, (...args: any[]) => any>;
|
||||
/** Пункт сразу в главном меню, без подменю отчёта. */
|
||||
topLevel?: boolean;
|
||||
requiredConfigKeys?: string[];
|
||||
requiredScriptProperties?: string[];
|
||||
htmlFiles?: string[];
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
@ -17,6 +17,7 @@ describe('salesOriginal profile', () => {
|
|||
'completed',
|
||||
'effectiveness',
|
||||
'newTemplates',
|
||||
'deleteOldReports',
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ describe('universal profile', () => {
|
|||
'currentTasks',
|
||||
'effectiveness',
|
||||
'newTemplates',
|
||||
'deleteOldReports',
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue