-2

I am using a trigger for each row come in, it will convert to pdf from excel and send the email straightaway.

In PDF that I need to convert, there's include photo. In order to convert, the time from trigger and convert photo must be like 1 sec or half so the photo turn up half, I believe it not finished converting.

So, I need the script that can pause from running like 3 seconds and continue the script afterward so that the photo are completely load.

var changedFlag = false;
var TEMPLATESHEET='Boom-Report';

function emailSpreadsheetAsPDF() {

//********************LIKE I NEED TO PAUSE FOR 3 SECONDS HERE*****************////
  DocumentApp.getActiveDocument();
  DriveApp.getFiles();

  // This is the link to my spreadsheet with the Form responses and the Invoice Template sheets
  // Add the link to your spreadsheet here 
  // or you can just replace the text in the link between "d/" and "/edit"
  // In my case is the text: 17I8-QDce0Nug7amrZeYTB3IYbGCGxvUj-XMt8uUUyvI
  const ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1NVJOdFLBAgNFqSHhnHJYybjUlSqhv4hKI_HXJyhJ88E/edit");

  // We are going to get the email address from the cell "B7" from the "Invoice" sheet
  // Change the reference of the cell or the name of the sheet if it is different
  const value = ss.getSheetByName("Source Email-Boom").getRange("X3").getValue();
  const email = value.toString();

  // Subject of the email message
  const subject = ss.getSheetByName("Source Email-Boom").getRange("B3").getValue();

    // Email Text. You can add HTML code here - see ctrlq.org/html-mail
  const body = "Boom Lifts Inspection Report - Sent via Auto Generate PDI Report from Glideapps";

  // Again, the URL to your spreadsheet but now with "/export" at the end
  // Change it to the link of your spreadsheet, but leave the "/export"
  const url = 'https://docs.google.com/spreadsheets/d/1NVJOdFLBAgNFqSHhnHJYybjUlSqhv4hKI_HXJyhJ88E/export?';

  const exportOptions =
    'exportFormat=pdf&format=pdf' + // export as pdf
    '&size=A4' + // paper size letter / You can use A4 or legal
    '&portrait=true' + // orientation portal, use false for landscape
    '&fitw=true' + // fit to page width false, to get the actual size
    '&sheetnames=false&printtitle=false' + // hide optional headers and footers
    '&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
    '&fzr=false' + // do not repeat row headers (frozen rows) on each page
    '&gid=671631174'; // the sheet's Id. Change it to your sheet ID.
    // You can find the sheet ID in the link bar. 
  // Select the sheet that you want to print and check the link,
  // the gid number of the sheet is on the end of your link.
  
  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
  
  // Generate the PDF file
  var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
  
  // Send the PDF file as an attachement 
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments: [{
            fileName: ss.getSheetByName("Source Email-Boom").getRange("B3").getValue().toString() +".pdf",
            content: response.getBytes(),
            mimeType: "application/pdf"
        }]
    });

  // Save the PDF to Drive. (in the folder) The name of the PDF is going to be the name of the Company (cell B5)
  const nameFile = ss.getSheetByName("Source Email-Boom").getRange("B3").getValue().toString() +".pdf"
  const folderID = "1ZKWq9jWmeEQlxncuTPHssCFXC3Fidmxn";
  DriveApp.getFolderById(folderID).createFile(response).setName(nameFile);
}


function on_sheet_change(event) {
  var sheetname = event.source.getActiveSheet().getName();
  var sheet = event.source.getActiveSheet();
  
  if (sheetname == 'Boom-Report') {
    emailSpreadsheetAsPDF() ;
  } else return;
}

Just now i have try what @corren and @que pointed out . But I realized that is not the main problem. My google sheet looks like this.

Google sheet

But after appscript running the Pdf turn up the picture like this.

enter image description here

and the formula i've been using in the image cell is

=arrayformula(image('Source Email-Boom'!CX3,1))

I believe it cause the size of the image was very huge. I'm not sure how to compressed. you guys any ideas ?

Biha
  • 97
  • 5
  • Have you tried using [Utilities.sleep()](https://developers.google.com/apps-script/reference/utilities/utilities#sleep(Integer)) – Cooper Dec 29 '21 at 03:08
  • @Cooper so i have to add Utilities.sleep(3) - as i need 3 seconds pause ? – Biha Dec 29 '21 at 03:11
  • 1
    Try reading the link – Cooper Dec 29 '21 at 03:12
  • @Cooper i have updated the result and problem . Kindly have a look – Biha Dec 29 '21 at 03:52
  • 1
    Please post you image compression question as another question. We wish to keep our questions well defined and focused so that others can find them when they have a similar problem. – Cooper Dec 29 '21 at 04:01
  • 1
    Does this answer your question? [Why do we use SpreadsheetApp.flush();?](https://stackoverflow.com/questions/41175326/why-do-we-use-spreadsheetapp-flush) – Rafa Guillermo Dec 29 '21 at 08:28

1 Answers1

0

As Cooper pointed out, try Utilities.sleep()

The parameter is stated in milliseconds, so for a 3-second delay, you can add this to your script where the pause is desired:

Utilities.sleep(3000)
que
  • 121
  • 1
  • 8