# Image to Markdown (Google Suite) [Google Apps Script](https://script.google.com/u/3/home/projects/169GsvT89Wu_RdCIYmHUCo2Zlk8cs9a-p18dox_T50eDREMZnD_h0ViG3) ``` function generateMarkdownForNewImages() { const folderId = "1XDS0BokxPcStz-LpKyf1aLb8jgP7fa_d"; // Your folder ID const ss = SpreadsheetApp.openById("18DL8QWtqBBog8n7lWgE4dvJW9o2W3hIrnkNN8bZrGyY"); // Your spreadsheet ID const sheet = ss.getSheetByName("Sheet1"); // Adjust the sheet name if necessary if (!sheet) { Logger.log("Error: Sheet 'Sheet1' not found."); return; } try { const folder = DriveApp.getFolderById(folderId); // Access the folder // Get existing file IDs to avoid duplicates const lastRow = sheet.getLastRow(); let existingFileIds = []; if (lastRow >= 2) { // There is data beyond the header row const dataRange = sheet.getRange(2, 4, lastRow - 1, 1); // Column D (File ID) const data = dataRange.getValues(); existingFileIds = data.flat().map(id => id.toString()); } const files = folder.getFiles(); // Collect new rows in an array const newRows = []; while (files.hasNext()) { const file = files.next(); const fileId = file.getId().toString(); // Check if the file has already been processed if (!existingFileIds.includes(fileId)) { const fileName = file.getName(); // Set file permissions to public if not already set const access = file.getSharingAccess(); const permission = file.getSharingPermission(); if (access !== DriveApp.Access.ANYONE_WITH_LINK || permission !== DriveApp.Permission.VIEW) { file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW); } // Construct the public URL using the thumbnail endpoint const publicUrl = "https://drive.google.com/thumbnail?id=" + fileId + "&sz=w1000"; const markdownLink = `![${fileName}](${publicUrl})`; // Prepare the row data const row = [ `=IMAGE("${publicUrl}", 4, 100, 100)`, // Thumbnail formula fileName, // File name markdownLink, // Markdown link fileId, // File ID new Date().toISOString() // Timestamp when processed ]; newRows.push(row); // Add the file ID to the list of processed files existingFileIds.push(fileId); } } // Append all new rows to the sheet at once if (newRows.length > 0) { const startRow = sheet.getLastRow() + 1; const numRows = newRows.length; const numCols = newRows[0].length; sheet.getRange(startRow, 1, numRows, numCols).setValues(newRows); } } catch (error) { Logger.log("Error: " + error.message); } } ``` ## How it works - When you upload an Image to "[Ihub_Show](https://drive.google.com/drive/u/3/folders/1XDS0BokxPcStz-LpKyf1aLb8jgP7fa_d)", within 5 minutes, the script is run and it uploads the image thumbnail and a markdown acessible embed link to [Img-to-markdown](https://docs.google.com/spreadsheets/d/18DL8QWtqBBog8n7lWgE4dvJW9o2W3hIrnkNN8bZrGyY/edit?gid=0#gid=0) - You can then copy that link and paste it directly into your HackMD file! ![PXL_20241024_165930616.jpg](https://drive.google.com/thumbnail?id=13Ln6GirYZULjg3q32JS8SXK8OgxXWBfB&sz=w1000) ![PXL_20241024_180333592.jpg](https://drive.google.com/thumbnail?id=13M2_lHZpMdOXWiI4b58KV2Ww2T9wqoNF&sz=w1000) ![PXL_20241029_154253282.jpg](https://drive.google.com/thumbnail?id=1664I-cud1pj6IC-wAsHgSNwZJoDvwa4r&sz=w1000)