-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogLibrary.js
More file actions
72 lines (62 loc) · 2.39 KB
/
LogLibrary.js
File metadata and controls
72 lines (62 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Initialise le système de journalisation.
* @param {string} fileId - L'ID du fichier Google Doc à utiliser comme journal.
* @return {string} - 'OK' si le fichier est trouvé, ou un message d'erreur.
*/
function InitializeLogging(fileId) {
Logger.log('Starting InitializeLogging with fileId: ' + fileId);
// Check if the same file is already initialized
var currentLogFileId = PropertiesService.getScriptProperties().getProperty('LogFileID');
if (currentLogFileId === fileId) {
Logger.log('System already initialized with this log file');
return 'OK';
}
// If ID is different or non-existent, proceed with initialization
PropertiesService.getScriptProperties().deleteAllProperties();
Logger.log('Properties reset completed');
try {
Logger.log('Attempting to retrieve file...');
var fileFound = DriveApp.getFileById(fileId);
Logger.log('File found: ' + fileFound.getName());
if (!fileFound) {
Logger.log('Error: fileFound is null or undefined');
throw 'File not found with ID: ' + fileId;
}
Logger.log('Saving file ID in properties...');
PropertiesService.getScriptProperties().setProperty('LogFileID', fileId);
Logger.log('ID successfully saved');
return 'OK';
} catch (e) {
Logger.log('Caught error: ' + e.toString());
return e.toString();
}
}
/**
* Enregistre les logs actuels dans le fichier journal.
*/
function fnSaveLog() {
var logFileID = PropertiesService.getScriptProperties().getProperty('LogFileID');
if (!logFileID) {
Logger.log('No log file initialized.');
return;
}
var logContent = Logger.getLog(); // Récupère les logs actuels
if (trim_(logContent) !== "") {
var logDoc = DocumentApp.openById(logFileID);
var body = logDoc.getBody();
body.insertParagraph(0, logContent); // Ajoute les logs en haut du fichier
logDoc.saveAndClose();
Logger.clear(); // Vide les logs
}
}
/**
* Trim une chaîne de caractères (supprime les espaces en début et fin).
* @param {string} stringToTrim - Chaîne à nettoyer.
* @return {string} - Chaîne nettoyée.
*/
function trim_(stringToTrim) {
if (typeof stringToTrim === 'string') {
return stringToTrim.replace(/^\s+|\s+$/g, "");
}
return stringToTrim;
}