创建多个PDF但不是全部正确创建

时间:2017-11-06 22:34:18

标签: google-apps-script

我在下面创建了这个脚本,将一个单元格的值和页面的PDF更改为文档。它运作良好,但只有前5或6个PDF正在创建,其余的似乎是'抽奖运气'。有些是好的,其他人给我一个HTML错误消息。

这是我的代码:

function CreateClassPacks() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.

  var foldersave=DriveApp.getFolderById('xxxxxxxxxxxxxxxxxxxxx');
  var d= new Date();
  var dateStamp = d.getDate()+"/"+d.getMonth()+"/"+d.getYear();

  var request = {
    "method": "GET",
    "headers":{"Authorization": "Bearer "+ScriptApp.getOAuthToken()},    
    "muteHttpExceptions": true
  };

  var key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 
  var fetch='https://docs.google.com/spreadsheets/d/'+key+'/export?format=pdf&size=A4&portrait=false'

  var classCodeSheetNum = 0
  var dataRange = SpreadsheetApp.getActiveSpreadsheet().getSheets()[classCodeSheetNum].getRange(2, 1, 10, 1);
  var data = dataRange.getValues();
  Logger.log(data)

  var sheetNum = 1

  for (var r=0; r<(data.length)-1; r++) { 
    for (i in data[0]) {
      SpreadsheetApp.getActiveSpreadsheet().getSheets()[sheetNum].getRange('A2').setValue(data[r][i]);

      var source = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = source.getSheets()[sheetNum];

      var classCode = sheet.getRange("A2").getValue();

      for(var w=0; w< sheetNum;w++)
      {
        sheet = source.getSheets()[w];
        sheet.hideSheet();
      }

      var name = classCode + " " + dateStamp + ".pdf";
      var pdf = UrlFetchApp.fetch(fetch, request);
      pdf = pdf.getBlob().setName(name);
      var file = foldersave.createFile(pdf)

      for(var q=0; q< sheetNum;q++)
      {
        sheet = source.getSheets()[q];
        sheet.showSheet();
      }
    }
  }
}

这就是我取代未正确创建为PDF的文件:

<!DOCTYPE html><html lang="en"><head><meta name="description" content="Web word processing, presentations and spreadsheets"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0"><link rel="shortcut icon" href="//ssl.gstatic.com/docs/common/drive_favicon1.ico"><title>Too Many Requests</title><link href="//fonts.googleapis.com/css?family=Product+Sans" rel="stylesheet" type="text/css"><style>/* Copyright 2017 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')}}</style><style type="text/css">body {background-color: #fff; font-family: Arial,sans-serif; font-size: 13px; margin: 0; padding: 0;}a, a:link, a:visited {color: #112ABB;}</style><style type="text/css">.errorMessage {font-size: 12pt; font-weight: bold; line-height: 150%;}</style></head><body><div id="outerContainer"><div id="innerContainer"><div style="position: absolute; top: -80px;"><div id="drive-logo"><a href="/"><span class="docs-drivelogo-img" title="Google logo"></span><span class="docs-drivelogo-text">&nbsp;Drive</span></a></div></div>Wow, this file is really popular! It might be unavailable until the crowd clears. <a href="">Try again</a>.</div></div></body><style>#outerContainer {margin: auto; max-width: 750px;}#innerContainer {margin-bottom: 20px; margin-left: 40px; margin-right: 40px; margin-top: 80px; position: relative;}</style></html>

如果有人可以提供帮助,我会很高兴,因为这会杀了我哈哈!

1 个答案:

答案 0 :(得分:0)

正如错误所说,这是因为您发送了太多请求。

在你的电话之间尝试大约30ms的睡眠时间:

Utilities.sleep(30);
var file = foldersave.createFile(pdf);

这可能会降低请求速度并可能有效。