-1

For example, I am using Google App Script to send emails, and the email addresses, email contents are from google sheet (sheet1),

enter image description here

Just an code example (if the footer image in Google drive)

function sendEmail() {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  
  var cell = ss.getActiveCell();
  var row = cell.getRow();
  var column = cell.getColumn();
  var lr = ss.getLastRow();

  for (var i = 2;i<=lr;i++){
  var assignmentName = sheet.getRange(i, 2).getValue();
  var emailAddress = sheet.getRange(i, 1).getValue();
  
  var image1 = DriveApp.getFileById("fileID").getBlob();
  var message = "Hi Mr/Mrs,<br /><br />Bellow is your info... <br><img src='cid:Footerimage'> <br>";

  
  GmailApp.sendEmail(emailAddress,"A Test Email", message, 
   {
     htmlBody: message,
     inlineImages:
      {
        Footerimage: image1
      }
    }
   );
}

my question is how can I reference this footer image in my google app script if the footer image is from a cell that in the same google sheet(sheet2).

enter image description here

Tanaike
  • 181,128
  • 11
  • 97
  • 165
UD.Cole
  • 45
  • 8
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand `send an email using this image from Google Apps Script`. Can I ask you about the detail of your goal? And, in your situation, the image is put into a cell? Or the image is put over cells? – Tanaike Jan 20 '22 at 00:04
  • Whenever possible, you need to include a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) that reproduces the issue. You can also include the expected behavior, the actual behavior, and how they differ, that would be helpful as well. Please visit [How to ask](https://stackoverflow.com/help/how-to-ask) to have some tips on how to write a question, so the community will be able to help you out in a better way. – David Salomon Jan 20 '22 at 00:16
  • @Tanaike I am sorry about my English. I rewrite the question and hope you can understand. – UD.Cole Jan 20 '22 at 01:19
  • @DavidSalomon I am sorry. I rewrite the question and hope it helps you to understand my question. – UD.Cole Jan 20 '22 at 01:20
  • Thank you for replying and adding more information. In your situation, how is the image put in the Spreadsheet? I thought that it is required to know that the image is put into a cell or the image is put over cells. – Tanaike Jan 20 '22 at 01:20
  • @Tanaike Hey Tanaike, I just insert this image into Google sheet manully. – UD.Cole Jan 20 '22 at 01:22
  • Thank you for replying. I have to apologize for my poor English skill, again. From `I just insert this image into Google sheet manully.`, I couldn't understand that the image is put into a cell or the image is put over cells. – Tanaike Jan 20 '22 at 01:25
  • @Tanaike click "Insert" --> Image --> Insert an image in a cell – UD.Cole Jan 20 '22 at 01:27
  • Thank you for replying. From your replying, I understood that the image is put into a cell (From your sample image, it's the cell "A1" of "Sheet2"). – Tanaike Jan 20 '22 at 01:28
  • @ Tanaike My bad, I am sorry. – UD.Cole Jan 20 '22 at 01:28
  • As another question, about `How can I reference this footer image based on google app script.`, how do you want to put the image? For example, is the image put as the inline image? And, it seems that your sample image of "Sheet1" has the multiple email addresses. But in your script, only one email address is used. In your goal, you want to send only one email? Can you update your question by including more information for helping users understand your question? – Tanaike Jan 20 '22 at 01:30
  • @Tanaike I need to put this image at the bottom place of the email, I am not sure how many ways we have. But I know inline image is a good idea, also I know how to use it if the image is from google drive. So my question is how can I reference this image in my email script that from Google sheet rather it is from Google Drive. – UD.Cole Jan 20 '22 at 01:35
  • Thank you for replying. I understand that you wanted to retrieve the image from the image embedded into a cell. In this case, you want to send only one email using the address and a name retrieved from the cells "A2:B2". Is my understanding correct? – Tanaike Jan 20 '22 at 01:38
  • @Tanaike yeah, you are right. I want to send emails that with footer image that I provided above, the image is from the same google sheet rather google drive. I know how I can apply image that from google drive based script, also how I can apply html structure. Well, there is bounch of code, what I need to do is just add piece of code to implement this. – UD.Cole Jan 20 '22 at 01:48
  • Thank you for replying. I noticed that your script was updated. You said `yeah, you are right.` for my question of `you want to send only one email using the address and a name retrieved from the cells "A2:B2".`. But, when I saw your updated question, it seems that you didn't want to send only one email. Now I'm confused to understand your question. I deeply apologize for my poor English skill. Can I ask you about your actual goal? – Tanaike Jan 20 '22 at 01:56
  • @Tanaike I am so sorry about my description. Now, I need to send emails to the addresses that is from the column of email, name is from column of name, now we ignore the email content. Then I would like to insert the footer image into my email structure, how can implement it through google apps script if the footer image is from the same google sheet. (I know how I could do it if the iamge is from google drive, but I do not know how I can do it if the footer image is from google sheet.) – UD.Cole Jan 20 '22 at 02:05
  • Thank you for replying. From your replying, I proposed a sample script as an answer. Could you please confirm it? If that was not useful, I apologize. – Tanaike Jan 20 '22 at 02:41
  • @Tanaike Great and thanks. I got a lot from your answer. – UD.Cole Jan 20 '22 at 03:07
  • Thank you for replying. I'm glad your issue was resolved. – Tanaike Jan 20 '22 at 03:20

2 Answers2

2

I believe your goal is as follows.

  • You want to retrieve the image data from the image embedded into a cell. And, you want to send an email using the retrieved image as the footer (in this case, you want to use the image as the inline image).

Issue and workaround:

Unfortunately, in the current stage, there is no method for directly retrieving the image data from the image embedded into a cell. So in this case, as a workaround, I would like to propose a sample script using the method I have answered here.

About your showing script, when getValue is used in a loop, the process cost becomes high. And when you use one image, var image1 = DriveApp.getFileById("fileID").getBlob(); is not required to be included in the loop.

Usage:

In this workaround, in order to retrieve the image data from the image embedded into a cell, a Google Apps Script library is used.

1. Install Google Apps Script library.

Please install the Google Apps Script library. About the method for installing the library, you can see it at here.

2. Sample script.

function myFunction() {
  // Retrieve image data from the image embeded into a cell.
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const res = DocsServiceApp.openBySpreadsheetId(ss.getId()).getSheetByName("Sheet2").getImages();
  const obj = res.find(({ range }) => range.a1Notation == "A1");
  if (!obj) throw new Error("Image was not found.");
  const imageBlob = obj.image.blob;

  // Retrieve email addresses and send emails.
  const sheet = ss.getSheetByName("Sheet1");
  const values = sheet.getRange("A2:B" + sheet.getLastRow()).getValues();
  values.forEach(([emailAddress, assignmentName]) => {
    // In your showing script, "assignmentName" is not used. 
    const sampleBody = "A Test Email";
    const message = "Hi Mr/Mrs,<br /><br />Bellow is your info... <br><img src='cid:Footerimage'> <br>";
    GmailApp.sendEmail(emailAddress, "A Test Email", message, {
      htmlBody: message,
      inlineImages: { Footerimage: imageBlob }
    });
  });
}
  • When this script is run, the image data is retrieved from the cell "A1" of "Sheet2" and the emails are sent using the email addresses retrieved from the column "A" of "Sheet1". At that time, the image data is appended to the email as the footer using the inline-image.

  • From your showing script, message is sent as both the text body and the HTML body. Please be careful this.

Note:

  • In this sample script, it supposes that the email addresses are put to the column "A" of "Sheet1" and the image data is put to a cell "A1" of "Sheet2". When you changed this situation, the script might not be able to be used. Please be careful about this.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Hey, thanks. Just wonder that what does the symbol '=>' mean in your script above? – UD.Cole Jan 20 '22 at 03:09
  • About `what does the symbol '=>' mean in your script above?`, that is the arrow function. You can see the detail of it at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Tanaike Jan 20 '22 at 03:20
0

to send html content you must use params like this:

  GmailApp.sendEmail(emailAddress,"A Test Email", "",{htmlBody:message})

About the image, you have to get a way of debugging how it comes from the spreadsheet. Perhaps it comes as as a base64encoded, depending of that, we could help you.

Get the image from the spreadsheet using the cell address directly, like this getRange("A1").getValue()

Using the IDE, check if there is a method like getValueAsUrl(), something that could help you out.

Another thing, what about you put the image by url into the spreadsheet instead of directly? This way you could send that image as html object

Lucas Vale
  • 19
  • 4
  • What's coming from the spreadsheet? A blob? String? Could you console.log it and describe that, please. – Lucas Vale Jan 20 '22 at 01:54
  • hey I have tried getRange("A1").getValue() and the return value from Logs is "CellImage" – UD.Cole Jan 20 '22 at 01:57
  • I'm not on laptop now but using the IDE, try to find some method like getAsBlob() or getImage() right after the getValue, example getValue().getImage() – Lucas Vale Jan 20 '22 at 02:04