Home > Net >  Returned Error 400 Request failed Apps script
Returned Error 400 Request failed Apps script

Time:04-08

I have this code that I use to create PDF from spreadsheets and I modified it to use it in another spreadsheet and now I'm getting this error.

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

The full error seems to be:

Page Not Found/* Copyright 2022 Google Inc. All Rights Reserved. */

.goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block}* html .goog-inline-block{display:inline}*:first-child html .goog-inline-block{display:inline}#drive-logo{margin:18px 0;position:absolute;white-space:nowrap}.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png');background-size:116px 41px;display:inline-block;height:41px;vertical-align:bottom;width:116px}.docs-drivelogo-text{color:#000;display:inline-block;opacity:0.54;text-decoration:none;font-family:'Product Sans',Arial,Helvetica,sans-serif;font-size:32px;text-rendering:optimizeLegibility;position:relative;top:-6px;left:-7px;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:144dpi){.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_116x41dp.png')}}body {background-color: #fff; font-family: Arial,sans-serif; font-size: 13px; margin: 0; padding: 0;}a, a:link, a:visited {color: #112ABB;}.errorMessage {font-size: 12pt; font-weight: bold; line-height: 150%;}Sorry, unable to open the file at this time.

Please check the address and try again.

Get stuff done with Google Drive

Apps in Google Drive make it easy to create, store and share online documents, spreadsheets, presentations and more.

Learn more at drive.google.com/start/apps.

html {height: 100%; overflow: auto;}body {height: 100%; overflow: auto;}#outerContainer {margin: auto; max-width: 750px;}#innerContainer {margin-bottom: 20px; margin-left: 40px; margin-right: 40px; margin-top: 80px; position: relative;}

Is there a way I can solve this error? Here's the script:

  var ss2 = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1cEFEmTuVisjOvOpN1JieOOIvJloYcgFHQmD5a07lnCo/edit#gid=1149589669');   
  var sheetName = ss2.getSheetByName('Exportación_pdf');
  var sheetName2 = ss2.getSheetByName('Selección pedido');
  var folderID = "14QBHZ5268OI9cqurbcIuvDfa3GG6HUVa"; // Folder id to save in a folder.
  var num_rows = sheetName.getRange("AR1").getValue();
  var folder = DriveApp.getFolderById(folderID); 
  var pdfName = ss2.getSheetByName('Exportación_pdf').getRange('F1:F3').getValue()   "_"   ss2.getSheetByName('Exportación_pdf').getRange('AK5').getValue()   "_"   ss2.getSheetByName('Selección pedido').getRange('Q4').getValue(); // Nombre del documento
  var bogus = DriveApp.getRootFolder();
SpreadsheetApp.getActiveSpreadsheet().toast('Creando PDF');

  // export url
  var url = 'https://docs.google.com/spreadsheets/d/' ss2.getId() '/export?exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
    '&range=Exportacion_pdf!AJ1:AQ' num_rows
    '&size=A4'                           // paper size legal / letter / A4
    '&portrait=false'                     // orientation, false for landscape
    '&fitw=true'                        // fit to page width, false for actual size
    '&sheetnames=false&printtitle=false' // hide optional headers and footers
    '&pagenumbers=true&gridlines=false' // hide page numbers and gridlines
    '&fzr=true'                         // do not repeat row headers (frozen rows) on each page
    '&top_margin=0.5&bottom_margin=0.5&left_margin=0.5&right_margin=0.5'
    '&gid=' ss2.getSheetId();    // the sheet's Id

  var token = ScriptApp.getOAuthToken();

  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true,
    headers: {
      'Authorization': 'Bearer '    token
    }
  });

  var theBlob = response.getBlob().setName(pdfName '.pdf');

  // delete pdf if already exists
  var files = folder.getFilesByName(pdfName);
  while (files.hasNext())
  {
    files.next().setTrashed(true);
  }

  // create pdf
  var newFile = folder.createFile(theBlob);

  return true;

 }```

Thank you so much in advance!

CodePudding user response:

Change this:

var url = 'https://docs.google.com/spreadsheets/d/' sheetName '/export?exportFormat=pdf&format=pdf'

To this:

var url = 'https://docs.google.com/spreadsheets/d/' ss2.getId() '/export?exportFormat=pdf&format=pdf'

Seems you are not adding the Google Sheet id to the URL.

  • Related