简体   繁体   中英

Wrap text using input from Google Sheet (Google Apps Script)

I have a script that produces an automated email from cell content in a Google Sheet. Is it possible to restrict the width of a cell's output in the email, forcing the text to wrap? I have tried using textarea tags as follows:

+ <textarea rows="4" cols="20">

+ sheet.getRange(6,9,1,1).getValue()   

+ </textarea>

However, this simply outputs as "+ sheet.getRange(6,9,1,1).getValue() +" (ie it doesn't generate the cell content).

Is this possible?

Here's how I have built the script:

function EmailFormConfirmation() { 

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  Utilities.sleep(60000);

  var sheet = ss.getSheetByName("Form responses");

  var lock = LockService.getPublicLock();

  lock.waitLock(60000);

  lock.releaseLock();

  var email = sheet.getRange(2,9,1,1).getValue();  

  var message = "<HTML><BODY>"

    + "<P >Hi " 

    + sheet.getRange(4,9,1,1).getValue()

    + ","

    etc.

EDIT The below produces the cell content, but doesn't wrap the text.

var htmlMsg = "<HTML><BODY>"

    + "<textarea rows='4' cols='10'>"

    + sheet.getRange(6,9,1,1).getValue()   

    + "</textarea>"

    + "</HTML></BODY>";


    MailApp.sendEmail(email, "LMI Request", "", {htmlBody: htmlMsg}); 

To respond to the question re: textbox, the following did as you requested

  var htmlMsg = "<HTML><BODY>"
  + "<textarea rows='4' cols='20'>"
  + sheet.getRange(6,9,1,1).getValue()
  + "</textarea>"
  + "</BODY></HTML>";
  GmailApp.sendEmail("m....l@gmail.com", "subject","hi" , {htmlBody: htmlMsg});

note how the single and double quotes are used.

Here is an example of how you can use templates to build your email responses. The documentation can be found here: https://developers.google.com/apps-script/guides/html/templates

In code.gs

function myFunction(){
  var sheet = SpreadsheetApp .....
  var value = sheet.getRange(6,9,1,1).getValue();
  var emailHtml = buildTemplate(value);
   GmailApp.sendEmail("person@example.com", "subject","Hello", {htmlBody: emailHtml});  
}

function buildTemplate(values){  
  var template = HtmlService.createTemplateFromFile('emailTemplate');
  template.tmpValues = values;
  return template.evaluate().getContent();
}

in emailTemplate.html

<html>
  <body>
       <textarea rows='4' cols='20'>
         <?=tmpValues?>
       </textarea>
  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM