Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 随机‘;限制’;可以将电子表格导出为PDF的次数_Google Apps Script_Web Applications - Fatal编程技术网

Google apps script 随机‘;限制’;可以将电子表格导出为PDF的次数

Google apps script 随机‘;限制’;可以将电子表格导出为PDF的次数,google-apps-script,web-applications,Google Apps Script,Web Applications,[Solved]正如@ziganotschka所建议的那样-在代码失败时添加一个try-and-catch语句,并提供一个体验式的睡眠时间 我有一个简单的脚本,可以修改电子表格,然后将其“导出”为PDF格式,然后再次修改同一张表格并再次导出,具体取决于它需要循环多少次 因此,当它运行时,我有46个查询排队 有时它出口4 有时它出口8 有时候更, 有时更少 尽管一切都没有改变 我尝试过添加实用程序;再次上升到5000,有时有帮助,有时有阻碍 在导出为PDF时是否存在某种我不知道的限制 我做错什么了

[Solved]正如@ziganotschka所建议的那样-在代码失败时添加一个try-and-catch语句,并提供一个体验式的睡眠时间

我有一个简单的脚本,可以修改电子表格,然后将其“导出”为PDF格式,然后再次修改同一张表格并再次导出,具体取决于它需要循环多少次

因此,当它运行时,我有46个查询排队 有时它出口4 有时它出口8 有时候更, 有时更少

尽管一切都没有改变

我尝试过添加实用程序;再次上升到5000,有时有帮助,有时有阻碍

在导出为PDF时是否存在某种我不知道的限制

我做错什么了吗

var docurl=UrlFetchApp.fetch(URL,{headers:{'Authorization':'Bearer'+令牌,muteHttpExceptions:true,}})上发生错误

在generatePdf处未捕获(代码:531)(文件名) 在用户点击时(代码:299)(文件名)

Html页面有一个按钮和单击按钮时传递的字段

function userClicked(userInfo){

//Start the main Loop for each new pdf to be made 

//Sheet modification

//in an attempt at making the loop run more times
    var sleepDuration = 0; 
           if (loopI >= 6) {
             sleepDuration = 1500;
       }

       Utilities.sleep(sleepDuration);

       if (typeof subFolderExist == 'undefined') {
        subFolderExist = generatePdf(sheetid, Title[1], varName);
       } else {
         //Sub Folder Exists -  do nothing.
         // varTrash is only there to call the function and isn't used 
//anywhere else 
         var varTrash = generatePdf(sheetid, Title[1], varName, 
  subFolderExist);
       }
}





function generatePdf(sourceSpreadsheet, sheetName, Name, varifSubFolderExist) {


  var sourceSpreadsheetId = sourceSpreadsheet.getId();
  var ss = SpreadsheetApp.openById(sourceSpreadsheetId);
  var nameoffile = ss.getSheetByName('W1-D1').getRange(1, 1).getValue();  
      var theurl = 'https://docs.google.com/spreadsheets/d/'  

      + '' + sourceSpreadsheetId + '' //the file ID
      + '/export?exportFormat=pdf&format=pdf'
      + '&size=LETTER'
      + '&portrait=true'
      + '&fitw=true'       
      + '&top_margin=0.50'              
      + '&bottom_margin=0.50'          
      + '&left_margin=0.50'             
      + '&right_margin=0.50'           
      + '&sheetnames=false&printtitle=false'
      + '&pagenum=false'
      + '&gridlines=false'
      + '&fzr=FALSE'      
      + '&gid='
      + '1304619048';  




    // Get folder containing spreadsheet to save pdf in.
    var parents = DriveApp.getFileById(sourceSpreadsheetId).getParents();
    if (parents.hasNext()) {
      var folder = parents.next();
    }
    else {
      folder = DriveApp.getRootFolder();
    }



     //Create SubFolder
     var subFolder;
      if(typeof varifSubFolderExist === "undefined") {


     var today = new Date();
     var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
     var hours = today.getHours();
     var timePeriod;

         if (hours > 12) {
          hours = (hours - 12);
           timePeriod = "PM";
          }

       timePeriod = "AM";

       var time = hours + ":" + today.getMinutes() + ":" + today.getSeconds();
       var dateTime = date+' '+time + timePeriod;

       // Set the output folder name.
       var folderName = "PDFs - [" + dateTime + "]";


       subFolder = folder.createFolder(folderName);
       // Add Unique String
    } else {
       subFolder = DriveApp.getFolderById(varifSubFolderExist[0]);
   }
  //End Create SubFolder


      var token = ScriptApp.getOAuthToken();

      var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' +  token, muteHttpExceptions: true,} });
      //var fileid = DriveApp.createFile(docurl.getBlob()).setName(nameoffile+ '.pdf').getId();
      var fileid = subFolder.createFile(docurl.getBlob()).setName(sheetName + " " + Name + '.pdf').getId();

      var pdf = docurl.getBlob().setName(nameoffile + '.pdf');
      var filetodel = DriveApp.getFileById(fileid);

  return [subFolder.getId(), folderName];

}

正如@ziganotschka所建议的,添加一个try-and-catch语句,在代码失败时以指数形式增加睡眠,效果非常好。

您能分享您的代码和电子表格吗?欢迎来到StackOverFlow,请借此机会学习如何和。您可能需要添加
SpreadsheetApp.flush()
由于这是一个Web应用程序,而不是嵌入到google表单中,我不确定您是否可以使用
电子表格应用程序.flush()
这不是真的吗?@ziganotschka实现了一个
try…catch
语句,非常感谢您的帮助:)
function userClicked(userInfo){

//Start the main Loop for each new pdf to be made 

//Sheet modification

//in an attempt at making the loop run more times
    var sleepDuration = 0; 
           if (loopI >= 6) {
             sleepDuration = 1500;
       }

       Utilities.sleep(sleepDuration);

       if (typeof subFolderExist == 'undefined') {
        subFolderExist = generatePdf(sheetid, Title[1], varName);
       } else {
         //Sub Folder Exists -  do nothing.
         // varTrash is only there to call the function and isn't used 
//anywhere else 
         var varTrash = generatePdf(sheetid, Title[1], varName, 
  subFolderExist);
       }
}





function generatePdf(sourceSpreadsheet, sheetName, Name, varifSubFolderExist) {


  var sourceSpreadsheetId = sourceSpreadsheet.getId();
  var ss = SpreadsheetApp.openById(sourceSpreadsheetId);
  var nameoffile = ss.getSheetByName('W1-D1').getRange(1, 1).getValue();  
      var theurl = 'https://docs.google.com/spreadsheets/d/'  

      + '' + sourceSpreadsheetId + '' //the file ID
      + '/export?exportFormat=pdf&format=pdf'
      + '&size=LETTER'
      + '&portrait=true'
      + '&fitw=true'       
      + '&top_margin=0.50'              
      + '&bottom_margin=0.50'          
      + '&left_margin=0.50'             
      + '&right_margin=0.50'           
      + '&sheetnames=false&printtitle=false'
      + '&pagenum=false'
      + '&gridlines=false'
      + '&fzr=FALSE'      
      + '&gid='
      + '1304619048';  




    // Get folder containing spreadsheet to save pdf in.
    var parents = DriveApp.getFileById(sourceSpreadsheetId).getParents();
    if (parents.hasNext()) {
      var folder = parents.next();
    }
    else {
      folder = DriveApp.getRootFolder();
    }



     //Create SubFolder
     var subFolder;
      if(typeof varifSubFolderExist === "undefined") {


     var today = new Date();
     var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
     var hours = today.getHours();
     var timePeriod;

         if (hours > 12) {
          hours = (hours - 12);
           timePeriod = "PM";
          }

       timePeriod = "AM";

       var time = hours + ":" + today.getMinutes() + ":" + today.getSeconds();
       var dateTime = date+' '+time + timePeriod;

       // Set the output folder name.
       var folderName = "PDFs - [" + dateTime + "]";


       subFolder = folder.createFolder(folderName);
       // Add Unique String
    } else {
       subFolder = DriveApp.getFolderById(varifSubFolderExist[0]);
   }
  //End Create SubFolder


      var token = ScriptApp.getOAuthToken();

      var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' +  token, muteHttpExceptions: true,} });
      //var fileid = DriveApp.createFile(docurl.getBlob()).setName(nameoffile+ '.pdf').getId();
      var fileid = subFolder.createFile(docurl.getBlob()).setName(sheetName + " " + Name + '.pdf').getId();

      var pdf = docurl.getBlob().setName(nameoffile + '.pdf');
      var filetodel = DriveApp.getFileById(fileid);

  return [subFolder.getId(), folderName];

}