Google Appscript Format Batchs of Tabs Within A Google Sheet
Google Appscript Format Batchs of Tabs Within A Google Sheet
Google Appscript Format Batchs of Tabs Within A Google Sheet
com
08.OnTimeForTheExam (/LrfnS3vJ)
C# | 24 min ago | 1.42 KB
cc_harvest_turtle (/p4AB6ki2)
Lua | 30 min ago | 0.79 KB
2024-01-18_stats.json (/HXCjAS2i)
JSON | 58 min ago | 10.92 KB
Google Appscript format batchs of tabs within a Google Sheet SHARE
2024-01-18_stats.json (/MgbNV0sU)
CEDROID JAN
(/MESSAGE/COMPOSE? 286 0 NEVER ADD COMMENT (/LOGIN?
TWEET JSON | 60 min ago | 10.86 KB
2024-01-18_stats.json (/CysNpeMZ)
Not a member of Pastebin yet? Sign Up (/signup), it unlocks many cool features! JSON | 1 hour ago | 10.79 KB
1. /*
2. The success of the entire script, especially the formatting, hinges
on the correct setup of the getColumnFormats() function. It's
essential that the column names and their corresponding formats in
this function accurately reflect the actual data structure in your
sheets for the script to work effectively.
3.
4. The script below is based on a google sheet that contains hotel
reservations data but the column order and the cell formatting was
not correct, the issue was happening with hundreds of tabs within
the same sheet, modify the script according to your needs:
5.
6. 1. processSheets()
7. This is the main function that initiates the processing of all
sheets in the active spreadsheet. It performs several key tasks:
8.
Not a member of Pastebin yet?
9. - Retrieves all the sheets in the spreadsheet.
Sign Up (/signup), it unlocks many cool
10. - Creates or accesses the 'Formatting Logs' sheet for tracking
(/signup)features!
progress.
Petco
12. - Logs the processing status (success or error) for each sheet.
https://pastebin.com/Qdw8jUjc 1/7
1/18/24, 5:16 PM Google Appscript format batchs of tabs within a Google Sheet - Pastebin.com
API
13. - Displays a notification TOOLS
summarizing the numberFAQ
of sheets
PASTEBIN (/DOC_API)
successfully processed and those (/TOOLS)
with errors. (/FAQ)
paste LOGIN
(/)
14.
(/) (/LOGIN)
17.
18. - Checking if the sheet has any columns and rows.
19. - Sorting the columns alphabetically based on headers.
20. - Writing the sorted data back to the sheet.
21. - Applying formatting to each column based on predefined rules.
error).
29.
30. 5. setupLogSheet(logSheet)
31. - Sets up the initial headers in the 'Formatting Logs' sheet for
32.
33. 6. isSheetProcessed(logSheet, sheetName)
35.
36. 7. getColumnFormats()
37. - Crucial for the entire script, this function returns a hardcoded
and formats with the actual column names present in the sheets.
Failure to correctly match these will result in improper or failed
formatting. The formats for each type of data (like date, text,
number, currency) need to be predefined here.
41.
42. */
43.
https://pastebin.com/Qdw8jUjc 2/7
1/18/24, 5:16 PM Google Appscript format batchs of tabs within a Google Sheet - Pastebin.com
API
44. function processSheets() { TOOLS FAQ
PASTEBIN
45. (/DOC_API) (/TOOLS) (/FAQ)
var ss = SpreadsheetApp.getActiveSpreadsheet();
paste LOGIN
(/)
46. var sheets = ss.getSheets();
(/) (/LOGIN)
48. (/SIGNUP)
ss.insertSheet("Formatting Logs");
51. // Set up log sheet headers
52. setupLogSheet(logSheet);
53.
63.
64. if (status.error) {
65. errorCount++;
66. } else {
67. successCount++;
68. }
69.
70. processedCount++;
71. if (processedCount >= 30) {
74. }
75. }
76. });
77.
https://pastebin.com/Qdw8jUjc 3/7
1/18/24, 5:16 PM Google Appscript format batchs of tabs within a Google Sheet - Pastebin.com
(/)
92. if (lastColumn === 0 || lastRow === 0) {
(/) (/LOGIN)
101.
102. // Create a combined array of headers and data for sorting
103. var combined = headers.map(function(header, index) {
104. return [header].concat(data.map(function(row) {
113.
114. // Write the sorted data back to the sheet
115. for (var col = 0; col < combined.length; col++) {
116. sheet.getRange(1, col + 1,
lastRow).setValues(combined[col].map(function(cell, index) {
117. return [cell];
118. }));
119. }
120.
121. // Apply formats to the sorted columns and check for errors
122. combined.forEach(function(column, index) {
123. var header = column[0];
124. if (columnFormats[header]) {
125. try {
126. var range = sheet.getRange(2, index + 1, lastRow - 1);
127. applyFormat(range, columnFormats[header]);
https://pastebin.com/Qdw8jUjc 4/7
1/18/24, 5:16 PM Google Appscript format batchs of tabs within a Google Sheet - Pastebin.com
(/)
137. }
(/) (/LOGIN)
138. SIGN UP
150. break;
151. case "currency":
152. range.setNumberFormat(formatInfo.format);
153. break;
171. }
172.
173. function isSheetProcessed(logSheet, sheetName) { Not a member of Pastebin yet?
174. // Check if the sheet is already processed Sign Up (/signup), it unlocks many cool
175. var data = logSheet.getDataRange().getValues(); (/signup)features!
176. for (var i = 1; i < data.length; i++) {
177. if (data[i][0] === sheetName && data[i][3] === "Success") {
https://pastebin.com/Qdw8jUjc 5/7
1/18/24, 5:16 PM Google Appscript format batchs of tabs within a Google Sheet - Pastebin.com
(/)
182. }
(/) (/LOGIN)
183. SIGN UP
186. return {
187. "Date": { type: "date", format: "MM/dd/yyyy" },
188. "Name": { type: "text", format: "none" },
189. "Room": { type: "number", format: "0" },
194. };
195. }
196.
Advertisement
Add Comment
Advertisement
Ad
Not a member of Pastebin yet?
Sign Up (/signup), it unlocks many cool
(/signup)features!
https://pastebin.com/Qdw8jUjc 6/7
1/18/24, 5:16 PM Google Appscript format batchs of tabs within a Google Sheet - Pastebin.com
SIGN UP
create new paste (/) / syntax languages (/languages) / archive (/archive) / faq (/faq) / tools (/tools) / night mode (/SIGNUP)
(https://faceboo
(https:/
(/night_mode) / api (/doc_api) / scraping api (/doc_scraping_api) / news (/news) / pro (/pro)
privacy statement (/doc_privacy_statement) / cookies policy (/doc_cookies_policy) / terms of service
(/doc_terms_of_service) / security disclosure (/doc_security_disclosure) / dmca (/dmca) / report abuse (/report-abuse) /
contact (/contact)
By using Pastebin.com you agree to our cookies policy (/doc_cookies_policy) to enhance your experience.
Site design & logo © 2024 Pastebin
https://pastebin.com/Qdw8jUjc 7/7