Last active
March 14, 2025 19:43
Revisions
-
tanaikech revised this gist
Nov 22, 2022 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,6 +46,8 @@ function myFunction() { - When this script is run, a Gmail message is retrieved and it is converted to a PNG image. In this sample script, the PNG file is created in the root folder. - In this sample, the image size is 512 x 512 by `setDimensions(512, 512)`. If your HTML body is large, please modify this size for your actual situation. ## Reference - This sample script was asked to [this thread on Stackoverflow](https://stackoverflow.com/q/74506937). -
tanaikech created this gist
Nov 22, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ # Converting Gmail Message to Image using Google Apps Script  This is a workaround for converting a Gmail message to a PNG image using Google Apps Script. ## Sample script Please set the message ID of Gmail. ```javascript function myFunction() { var id = "###"; // Please set your message ID of Gmail. var message = GmailApp.getMessageById(id); var date = Utilities.formatDate( message.getDate(), Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm:ss" ); var from = message.getFrom(); var to = message.getTo(); var subject = message.getSubject(); var htmlBody = message.getBody(); var imageBlob = Charts.newTableChart() .setDataTable( Charts.newDataTable() .addColumn(Charts.ColumnType.STRING, "") .addRow([`<p style="font-size: 120%">Date: ${date}</p>`]) .addRow([`<p style="font-size: 120%">From: ${from}</p>`]) .addRow([`<p style="font-size: 120%">To: ${to}</p>`]) .addRow([`<p style="font-size: 120%">Subject: ${subject}</p>`]) .addRow([htmlBody]) .build() ) .setOption("allowHtml", true) .setDimensions(512, 512) .build() .getBlob(); DriveApp.createFile(imageBlob.setName("sample.png")); } ``` - In this sample script, the HTML body is used. - When this script is run, a Gmail message is retrieved and it is converted to a PNG image. In this sample script, the PNG file is created in the root folder. ## Reference - This sample script was asked to [this thread on Stackoverflow](https://stackoverflow.com/q/74506937).