返回错误 400 请求失败的 Apps 脚本

Returned Error 400 Request failed Apps script

我有这段代码可以用来从电子表格创建 PDF,我修改了它以便在另一个电子表格中使用它,但现在我收到了这个错误。

异常:https://docs.google.com 请求失败返回代码 400。截断服务器响应:

完整的错误似乎是:

找不到页面/* 版权所有 2022 Google Inc. 保留所有权利。 */

.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{背景图像:url('//ssl.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png');背景大小:116px 41px;显示:内联块;高度:41px;垂直对齐: 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 {背景颜色:#fff;字体系列:Arial,sans-serif;字体大小:13px;保证金:0;填充:0;}a,a:link,a:visited {颜色:#112ABB;}.errorMessage {字体大小:12pt;字体粗细:粗体;行高: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 {高度:100%;溢出:自动;}正文{高度:100%;溢出:自动;}#outerContainer {保证金:自动;最大宽度:750px;}#innerContainer {边距底部:20px;左边距:40px;右边距:40px;边距顶部:80px;位置:相对;}

有什么办法可以解决这个错误吗? 这是脚本:

  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!

改变这个:

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

为此:

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

您似乎没有将 Google Sheet id 添加到 URL。