'Exception: Request failed returned code 400 (French ?)

My reponse is :

Exception: Request failed for https://docs.google.com returned code 400. Truncated server response: <meta nam... (use muteHttpExceptions option to examine full response)

could anyone help me please ? Thank you ! (i'm french... sorry ;)

My code is :

 function envoiCopieFeuillePDF()
{
  DocumentApp.getActiveDocument();
  DriveApp.getFiles();

  // variables
  const doc = SpreadsheetApp.getActive();
  const docID = '1_Gt77USWTE3c-8dEzm-WGElxxxxxxxxxAA5bfX2vlHMrdo';
  const feuilleID = 'xxxxXXXxxxx';
  const email = '[email protected]';
  const dossier = DriveApp.getFolderById('1-1cN_xxxxxxvjDwWtr-xFdb2UX_CW8RRz') ;
  const d = Utilities.formatDate(new Date(), "GMT+1", "yyyyMMdd")
  const fichier = 'Relevé de factures' + "-" + d + ".pdf"
  const objet = 'TEST RELEVE pdf';
  const corps = "Veuillez trouver ci-joint TEST ...";

  // Création du fichier pdf
  const url = 'https://docs.google.com/spreadsheets/d/' + docID + '/export?';
  const exportOptions =
    'exportFormat=pdf&format=pdf' + 
    '&size=A4' + 
    '&portrait=false' +                     // orientation portrait, false pour paysage
    '&fitw=false' +                        // pas d'ajustement en largeur
    '&sheetnames=false&printtitle=false' + // pas de nom ni de titre à l'impression
    '&pagenumbers=false&gridlines=true' + // pas de numérotation, pas de grille
    '&fzr=false' +                         // frozen rows = pas de répétition de l'en-tête
    '&gid=' + feuilleID;
  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
  var reponse = UrlFetchApp.fetch(url + exportOptions, params).getBlob();


Solution 1:[1]

In your script, if the file of docID is the Spreadsheet, I thought that const feuilleID = 'Fairlight Avocat'; should be the sheet ID. I thought that this might be the reason for your issue. If you want to use the sheet name, how about the following modification?

From:

'&gid=' + feuilleID;

To:

'&gid=' + SpreadsheetApp.openById(docID).getSheetByName(feuilleID).getSheetId();

Note:

  • If you can directly use the sheet ID, please modify Fairlight Avocat of const feuilleID = 'Fairlight Avocat'; to the sheet ID.

  • If the file of docID is not Google Spreadsheet, please modify it to the file ID of Google Spreadsheet.

References:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tanaike