flowfix88

App Script GSheets 2- split to cells

Jun 11th, 2023 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.44 KB | None | 0 0
  1. ################ Extract Markdown Info: Prompt - Part 1 (extract settings) ###################
  2.  
  3. Create a JS script for google sheets to extract specific information from the Markdown text in the column A and splits it into separate columns in the sheet for that row.
  4.  
  5. The address of Google Sheet:
  6. https://docs.google.com/spreadsheets/d/1rFVnOALJjVzS-RUwVxt6TFwCvjOlyFFP3CFjXeBHtso/edit#gid=0
  7.  
  8. The name of an active sheet: ZW import.
  9.  
  10. The content to be extracted and splitted is located in the A2 cell and below cells (A3, A4, and so on).
  11.  
  12. The first part of the content to be extracted is located in the row that begins with "@ Main model used:". This row should be pulled in the respective row in the the column B.
  13.  
  14. The 2nd part of the content to be extracted is located in the row that begins with "@ OpenAI Status:". This row should be pulled in the respective row in the the column C.
  15.  
  16. The 3rd part of the content to be extracted is located in the row that begins with "@ Settings used:". This row should be pulled in the respective row in the the column D.
  17.  
  18. The 4rth part of the content to be extracted is located in the row that begins with "@ Midjourney AI Image Prompt:". This row should be pulled in the respective row in the the column E.
  19.  
  20. The 5th part of the content to be extracted is located in the row that begins with "@ Meta Description:". This row should be pulled in the respective row in the the column G.
  21.  
  22. The 6th part of the content to be extracted is located in the row that begins with "#". This row should be pulled in the respective row in the the column H.
  23.  
  24.  
  25. **************** Extract Markdown Info: Script - Part 1 (extract settings) **********************
  26.  
  27. ***
  28.  
  29. function onOpen() {
  30. var ui = SpreadsheetApp.getUi();
  31. ui.createMenu('Custom Menu')
  32. .addItem('Import Text Files', 'importTextFiles')
  33. .addItem('Extract Markdown Info', 'extractMarkdownInfo')
  34. .addToUi();
  35. }
  36.  
  37. function importTextFiles() {
  38. var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1rFVnOALJjVzS-RUwVxt6TFwCvjOlyFFP3CFjXeBHtso/edit#gid=0');
  39. var sheet = ss.getSheetByName('ZW import'); // Change Sheet1 to your sheet's name if it's different
  40. var folder = DriveApp.getFolderById('1o743ScrSFmnztwL5PBbxmd2mFclPiidO'); // Change 'folderID' with your Google Drive Folder ID where the text files are located
  41. var files = folder.getFilesByType(MimeType.PLAIN_TEXT);
  42. var i = 2; // Start at cell A2
  43. while (files.hasNext()) {
  44. var file = files.next();
  45. var text = file.getBlob().getDataAsString();
  46. sheet.getRange('A' + i++).setValue(text);
  47. }
  48. }
  49.  
  50. function extractMarkdownInfo() {
  51. var ss = SpreadsheetApp.getActiveSpreadsheet();
  52. var sheet = ss.getSheetByName('ZW import'); // change this to the name of your sheet if different
  53.  
  54. var dataRange = sheet.getRange("A2:A");
  55. var data = dataRange.getValues();
  56.  
  57. for (var i = 0; i < data.length; i++) {
  58. var markdownText = data[i][0];
  59. var lines = markdownText.split("\n");
  60.  
  61. for (var j = 0; j < lines.length; j++) {
  62. var line = lines[j];
  63.  
  64. if (line.startsWith("@ Main model used:")) {
  65. sheet.getRange(i + 2, 2).setValue(line.replace("@ Main model used:", "").trim());
  66. } else if (line.startsWith("@ OpenAI Status:")) {
  67. sheet.getRange(i + 2, 3).setValue(line.replace("@ OpenAI Status:", "").trim());
  68. } else if (line.startsWith("@ Settings used:")) {
  69. sheet.getRange(i + 2, 4).setValue(line.replace("@ Settings used:", "").trim());
  70. } else if (line.startsWith("@ Midjourney AI Image Prompt:")) {
  71. sheet.getRange(i + 2, 5).setValue(line.replace("@ Midjourney AI Image Prompt:", "").trim());
  72. } else if (line.startsWith("@ Meta Description:")) {
  73. sheet.getRange(i + 2, 7).setValue(line.replace("@ Meta Description:", "").trim());
  74. } else if (line.startsWith("# ")) {
  75. sheet.getRange(i + 2, 8).setValue(line.replace("#", "").trim());
  76. }
  77. }
  78. }
  79. }
  80.  
  81. ************************************************************************************************
  82.  
  83. ################ Extract Markdown Info: Prompt - Part 2 (extract headings FAQ + text) ###################
  84.  
  85. Create a JS script for google sheets to extract specific information from the Markdown text in the column A and splits it into separate columns in the sheet for that row.
  86.  
  87. The name of an active sheet: ZW import.
  88.  
  89. The first part of the content to be extracted is located between the row that begins with "##" and the row that begins with "## Frequently Asked Questions". The content of these rows consists of up to seven H2 Headings and respective text below each of them.
  90.  
  91. 1st H2 heading should be pulled into the respective row in the column J.
  92. 2nd H2 heading should be pulled into the respective row in the column L.
  93. 3rd H2 heading should be pulled into the respective row in the column N.
  94. 4th H2 heading should be pulled into the respective row in the column P.
  95. 5th H2 heading should be pulled into the respective row in the column R.
  96. 6th H2 heading should be pulled into the respective row in the column T.
  97. 7th H2 heading should be pulled into the respective row in the column V.
  98.  
  99. Do not extract empty lines.
  100.  
  101. the text below the 1st H2 heading should be pulled into the respective row in the column K.
  102. the text below the 2nd H2 heading should be pulled into the respective row in the column M.
  103. the text below the 3rd H2 heading should be pulled into the respective row in the column O.
  104. the text below the 4th H2 heading should be pulled into the respective row in the column Q.
  105. the text below the 5th H2 heading should be pulled into the respective row in the column S.
  106. the text below the 6th H2 heading should be pulled into the respective row in the column U.
  107. the text below the 7th H2 heading should be pulled into the respective row in the column W.
  108.  
  109. Do not extract empty lines.
  110.  
  111. The next part of the content to be extracted is located after the row that begins with "## Frequently Asked Questions". The content of these rows consists of up to five H3 Headings and respective text below each of them.
  112.  
  113. 1st H3 heading should be pulled into the respective row in the column X.
  114. 2nd H3 heading should be pulled into the respective row in the column Z.
  115. 3rd H3 heading should be pulled into the respective row in the column AB.
  116. 4th H3 heading should be pulled into the respective row in the column AD.
  117. 5th H3 heading should be pulled into the respective row in the column AF.
  118.  
  119. Do not extract empty lines.
  120.  
  121. the text below the 1st H2 heading should be pulled into the respective row in the column Y.
  122. the text below the 2nd H2 heading should be pulled into the respective row in the column AA.
  123. the text below the 3rd H2 heading should be pulled into the respective row in the column AC.
  124. the text below the 4th H2 heading should be pulled into the respective row in the column AE.
  125. the text below the 5th H2 heading should be pulled into the respective row in the column AF.
  126.  
  127. Do not extract empty lines.
  128.  
  129. #####################################################
  130.  
  131. **************** Extract Markdown Info: Script - Part 2 (extract headings FAQ + text + NO EMPTY LINES) **********************
  132. function extractFromMarkdown() {
  133. var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('ZW import');
  134. var markdowns = sheet.getRange('A1:A' + sheet.getLastRow()).getValues();
  135.  
  136. for (var i = 0; i < markdowns.length; i++) {
  137. var markdown = markdowns[i][0];
  138.  
  139. var faqIndex = markdown.indexOf('## Frequently Asked Questions');
  140. var part1 = markdown.substring(0, faqIndex);
  141. var part2 = markdown.substring(faqIndex);
  142.  
  143. var h2Sections = part1.split('\n## ').slice(1);
  144. for (var j = 0; j < h2Sections.length; j++) {
  145. var splitIndex = h2Sections[j].indexOf('\n');
  146. var title = h2Sections[j].substring(0, splitIndex).trim();
  147. var text = h2Sections[j].substring(splitIndex).trim().replace(/\n/g, ' ');
  148.  
  149. sheet.getRange(i + 1, 'J'.charCodeAt(0) - 64 + j * 2).setValue(title);
  150. sheet.getRange(i + 1, 'K'.charCodeAt(0) - 64 + j * 2).setValue(text);
  151. }
  152.  
  153. var h3Sections = part2.split('\n### ').slice(1);
  154. for (var j = 0; j < h3Sections.length; j++) {
  155. var splitIndex = h3Sections[j].indexOf('\n');
  156. var title = h3Sections[j].substring(0, splitIndex).trim();
  157. var text = h3Sections[j].substring(splitIndex).trim().replace(/\n/g, ' ');
  158.  
  159. sheet.getRange(i + 1, 'X'.charCodeAt(0) - 64 + j * 2).setValue(title);
  160. sheet.getRange(i + 1, 'Y'.charCodeAt(0) - 64 + j * 2).setValue(text);
  161. }
  162. }
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. ************************************************************************************************
  172. ################ Extract Markdown Info: Prompt - Part 3 (extract Intro text) ###################
  173.  
  174. Create a JS script for google sheets to extract specific information from the Markdown text in the column A and splits it into separate columns in the sheet for that row.
  175.  
  176. The name of an active sheet: ZW import.
  177.  
  178. The part of the content to be extracted is located between the row following the row that begins with "# " and the row that begins with "## ".
  179. Do not extract the content of the row that begins with "# ".
  180. Do not extract empty lines.
  181.  
  182. This extracted content should be pulled in the respective row in the the column I.
  183.  
  184. #####################################################
  185.  
  186. **************** Extract Markdown Info: Script - Part 3 (extract Intro text) **********************
  187. function processMarkdown() {
  188. var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ZW import");
  189. var data = sheet.getRange("A1:A" + sheet.getLastRow()).getValues();
  190.  
  191. for (var i = 0; i < data.length; i++) {
  192. var text = data[i][0];
  193. var result = "";
  194.  
  195. // match and extract the content
  196. var match = text.match(/#\s[\s\S]*?(?=\n##\s)/);
  197. if (match) {
  198. var lines = match[0].split('\n');
  199. for (var j = 1; j < lines.length; j++) { // start from 1 to exclude the line with "# "
  200. var line = lines[j].trim();
  201. if (line !== "") {
  202. result += line + '\n';
  203. }
  204. }
  205. }
  206. // write the result to the column I of the same row
  207. sheet.getRange(i + 1, 9).setValue(result);
  208. }
  209. }
  210.  
  211.  
  212. ###############################################
  213. ************************************************************************************************
  214. ################ Convert to HTML: Prompt - Part 4 (Convert to HTML) ###################
  215. Create a JS script for google sheets to:
  216. 1) Convert the text in the columns J, L, N, P, R, T, V, X, Z, AB, AD, AF into HTML markdown marked as H2 heading.
  217.  
  218. 2) Convert the text in the columns K, M, O, Q, S, U, W, Y, AA, AC, AE, AG into HTML markdown marked as paragraphs.
  219.  
  220.  
  221. #####################################################
  222.  
  223. **************** Convert to HTML: Prompt - Part 4 (Convert to HTML) **********************
  224. function convertToMarkdown() {
  225. var ss = SpreadsheetApp.getActiveSpreadsheet();
  226. var sheet = ss.getActiveSheet(); // Gets the current active sheet.
  227. var range = sheet.getDataRange(); // Gets the range of the data.
  228. var values = range.getValues(); // Gets the values of the cells in the range.
  229.  
  230. // Heading columns
  231. var headingColumns = ['J', 'L', 'N', 'P', 'R', 'T', 'V', 'X', 'Z', 'AB', 'AD', 'AF'];
  232. // Paragraph columns
  233. var paragraphColumns = ['K', 'M', 'O', 'Q', 'S', 'U', 'W', 'Y', 'AA', 'AC', 'AE', 'AG'];
  234.  
  235. // Convert to markdown
  236. for (var row = 0; row < values.length; row++) {
  237. for (var col = 0; col < values[row].length; col++) {
  238. var columnName = columnToLetter(col+1); // +1 as col is 0 indexed
  239.  
  240. // Check if the column is in the heading list, and if it contains any value
  241. if (headingColumns.includes(columnName) && values[row][col]) {
  242. // Convert the text in the cell to H2 heading in markdown
  243. values[row][col] = '<h2>' + values[row][col] + '</h2>';
  244. }
  245. // Check if the column is in the paragraph list, and if it contains any value
  246. else if (paragraphColumns.includes(columnName) && values[row][col]) {
  247. // Convert the text in the cell to paragraph in markdown
  248. values[row][col] = '<p>' + values[row][col] + '</p>';
  249. }
  250. }
  251. }
  252.  
  253. // Apply the changes
  254. range.setValues(values);
  255. }
  256.  
  257. // Function to convert column number to letter.
  258. function columnToLetter(column) {
  259. var temp, letter = '';
  260. while (column > 0) {
  261. temp = (column - 1) % 26;
  262. letter = String.fromCharCode(temp + 65) + letter;
  263. column = (column - temp - 1) / 26;
  264. }
  265. return letter;
  266. }
  267.  
  268.  
Advertisement
Add Comment
Please, Sign In to add comment