Home > Software engineering >  How have differents orientation for multiple attachments when i send mail with apps script?
How have differents orientation for multiple attachments when i send mail with apps script?

Time:04-02

I have a problem when sending attachments via the sendEmail function via the script apps. The purpose of the code below is to send three attachments (the first two in portrait format and the last one in landscape format).

The sending of mail is done well and I have my three attachments. However, the one that should be in landscape format remains in portrait format and I don't understand why.

I did change the options of the external url for this one from portrait to false. Despite this, nothing happens. I have searched the internet but I can't find any answer to this problem.

Could someone please help me?

Here is the code I use:

// Fonction permettant de créer la pièce joint de l'onglet "Taux de réalisation hebdo RS"
 function pjTauxRealisationHebdo(){
   // Déclaration des constantes et des variables
   const classeur = SpreadsheetApp.getActiveSpreadsheet();
   var sheetId1 = classeur.getSheetByName("Taux de réalisation hebdo RS").getSheetId();
   var url_base = classeur.getUrl().replace(/edit$/,'');
   var dateExport;

   // Création du format pour la date d'exportation
   var dateJour = new Date();
   var jour = dateJour.getUTCDate();
   if (jour < 10){ var nouveauJour = "0"   jour;} else {var nouveauJour = jour;}
   var mois = dateJour.getUTCMonth()   1;
   if (mois < 10){var nouveauMois = "0"   mois;} else {var nouveauMois = mois;}
   var annee = dateJour.getUTCFullYear();
   var dateExport = (nouveauJour   "-"   nouveauMois   "-"   annee).toString();

   var nomPDF = "Taux_Réalisation_Hebdo_" dateExport;

   // Création de l'url externe
   var url_ext1 = 'export?exportFormat=pdf&format=pdf'
        '&gid='   sheetId1;
         '&size=7'
         '&portrait=true'
         '&fitw=true'
         '&sheetnames=false&printtitle=false&pagenumbers=false'
         '&gridlines=true'
         '&fzr=false';

   // Création de la variable options
   var options = {
     headers: {
       'Authorization': 'Bearer '    ScriptApp.getOAuthToken(),
     }
   }
   var response = UrlFetchApp.fetch(url_base   url_ext1, options);
   return response.getBlob().setName(nomPDF   '.pdf');
 }

 // Fonction permettant de créer la pièce joint de l'onglet "Taux de conformité hebdo RS"
 function pjTauxConformiteHebdo(){
   // Déclaration des constantes et des variables
   const classeur = SpreadsheetApp.getActiveSpreadsheet();
  var sheetId1 = classeur.getSheetByName("Taux de conformité hebdo RS").getSheetId();
   var url_base = classeur.getUrl().replace(/edit$/,'');
   var dateExport;

   // Création du format pour la date d'exportation
   var dateJour = new Date();
   var jour = dateJour.getUTCDate();
   if (jour < 10){ var nouveauJour = "0"   jour;} else {var nouveauJour = jour;}
   var mois = dateJour.getUTCMonth()   1;
   if (mois < 10){var nouveauMois = "0"   mois;} else {var nouveauMois = mois;}
   var annee = dateJour.getUTCFullYear();
   var dateExport = (nouveauJour   "-"   nouveauMois   "-"   annee).toString();

   var nomPDF = "Taux_Conformité_Hebdo_" dateExport;

   // Cration de l'url externe
   var url_ext1 = 'export?exportFormat=pdf&format=pdf'
        '&gid='   sheetId1;
         '&size=7'
         '&portrait=true'
         '&fitw=true'
         '&sheetnames=false&printtitle=false&pagenumbers=false'
         '&gridlines=true'
         '&fzr=false';

  // Création de la variable options
   var options = {
     headers: {
       'Authorization': 'Bearer '    ScriptApp.getOAuthToken(),
     }
   }
   var response = UrlFetchApp.fetch(url_base   url_ext1, options);
   return response.getBlob().setName(nomPDF   '.pdf');
 }

 // Fonction permettant de créer la pièce joint de l'onglet "Taux de conformité hebdo RS"
 function pjTauxAnalyseTourneeUVC(){
   // Déclaration des constantes et des variables
   const classeur = SpreadsheetApp.getActiveSpreadsheet();
   var sheetId1 = classeur.getSheetByName("Analyse tournées UVC").getSheetId();
   var url_base = classeur.getUrl().replace(/edit$/,'');
   var dateExport;

   // Création du format pour la date d'exportation
   var dateJour = new Date();
   var jour = dateJour.getUTCDate();
   if (jour < 10){ var nouveauJour = "0"   jour;} else {var nouveauJour = jour;}
   var mois = dateJour.getUTCMonth()   1;
   if (mois < 10){var nouveauMois = "0"   mois;} else {var nouveauMois = mois;}
   var annee = dateJour.getUTCFullYear();
   var dateExport = (nouveauJour   "-"   nouveauMois   "-"   annee).toString();

   var nomPDF = "Analyse_Tournées_UVC" dateExport;

   // Création de l'url externe
   var url_ext1 = 'export?exportFormat=pdf&format=pdf'
        '&gid='   sheetId1;
         '&size=7'
         '&portrait=false'
         '&fitw=true'
         '&sheetnames=false&printtitle=false&pagenumbers=false'
         '&gridlines=true'
         '&fzr=false';

   // Création de la variable options
   var options = {
     headers: {
       'Authorization': 'Bearer '    ScriptApp.getOAuthToken(),
     }
   }
   var response = UrlFetchApp.fetch(url_base   url_ext1, options);
   return response.getBlob().setName(nomPDF   '.pdf');
 }


 function sendMail(){
   var att1 = pjTauxRealisationHebdo();
   var att2 = pjTauxConformiteHebdo();
   var att3 = pjTauxAnalyseTourneeUVC();
   MailApp.sendEmail('[email protected]','sujet','message',
    {attachments:[att1,att2, att3]});
 }

CodePudding user response:

In your script, how about the following modification? Please modify your pjTauxAnalyseTourneeUVC() as follows.

From:

var url_ext1 = 'export?exportFormat=pdf&format=pdf'
     '&gid='   sheetId1;
      '&size=7'
      '&portrait=false'
      '&fitw=true'
      '&sheetnames=false&printtitle=false&pagenumbers=false'
      '&gridlines=true'
      '&fzr=false';

To:

var url_ext1 = 'export?exportFormat=pdf&format=pdf'
      '&gid='   sheetId1
      '&size=7'
      '&portrait=false'
      '&fitw=true'
      '&sheetnames=false&printtitle=false&pagenumbers=false'
      '&gridlines=true'
      '&fzr=false';
  • In your script, url_ext1 is export?exportFormat=pdf&format=pdf&gid=###, because of ; of '&gid=' sheetId1;. By this, portrait=false and other parameters are not used. I think that this is the reason of your issue. So, please remove ; of '&gid=' sheetId1;.

Note:

  • When I saw your other functions of pjTauxRealisationHebdo() and pjTauxConformiteHebdo(), I can see '&gid=' sheetId1;. So, please remove ; of '&gid=' sheetId1;.
  • Related