-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriveDocLineImageManager.js
91 lines (75 loc) · 2.56 KB
/
DriveDocLineImageManager.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Replace 'YOUR_FOLDER_ID' with your actual Google Drive folder ID.
var FOLDER_ID = 'YOUR_FOLDER_ID';
/**
* Entry point to process Google Docs in the specified folder.
*/
function mainProcess() {
var mainFolder = DriveApp.getFolderById(FOLDER_ID);
// Fetch the image blob for later use
var imageBlob = getImageBlob(mainFolder);
processFilesInFolder(mainFolder, imageBlob);
}
/**
* Gets the image blob from the specified folder.
*/
function getImageBlob(folder) {
// Replace 'YOUR_IMAGE_NAME' with the iamge that has to be placed in the file, this iamge must be originally place in folder related to YOUR_FOLDER_ID
var imageFile = folder.getFilesByName('YOUR_IMAGE_NAME').next();
return imageFile.getBlob();
}
/**
* Recursively processes Google Docs in the folder and subfolders.
*/
function processFilesInFolder(folder, imageBlob) {
var files = folder.getFilesByType(MimeType.GOOGLE_DOCS);
while (files.hasNext()) {
var file = files.next();
var fileName = file.getName();
Logger.log('Processing file: ' + fileName);
if (isValidFileName(fileName)) {
try {
var doc = DocumentApp.openById(file.getId());
var body = doc.getBody();
updateDocument(doc, body, imageBlob);
doc.saveAndClose();
Utilities.sleep(5000);
} catch (e) {
Logger.log(`Error processing file ${fileName}: ${e.toString()}`);
}
}
}
// Recursively process subfolders
var subfolders = folder.getFolders();
while (subfolders.hasNext()) {
processFilesInFolder(subfolders.next(), imageBlob);
}
}
function isValidFileName(fileName) {
// Replace "Example of name starting with - " and "Example of anothername starting with - "with the names you want to search.
return fileName.startsWith("Example of name starting with - ") || fileName.startsWith("Example of anothername starting with - ");
}
function updateDocument(doc, body, imageBlob) {
removeHeader(doc);
formatText(body);
removeFirstLineFromBody(body);
prependImage(body, imageBlob);
}
function removeHeader(doc) {
var headers = doc.getHeader();
headers && headers.removeFromParent();
}
function formatText(body) {
// Replace 'YOUR_FONT_NAME' with the font you choose.
body.setFontFamily('YOUR_FONT_NAME');
const paragraphs = body.getParagraphs();
paragraphs.forEach(para => {
para.editAsText().setItalic(false);
});
}
function removeFirstLineFromBody(body) {
var firstElement = body.getChild(0);
firstElement && firstElement.removeFromParent();
}
function prependImage(body, imageBlob) {
body.insertImage(0, imageBlob);
}