工作GAS,现在执行无序

时间:2018-05-02 18:40:48

标签: javascript google-apps-script google-sheets

我有一个Google App脚本,可以从Google电子表格中收集数据,编译数据,添加封面,导出新创建的数据包的PDF,然后自行清理。该脚本已经完美地工作了两年,但几个月前它开始执行乱序。该脚本现在在完成最后一个工作表之前执行导出到PDF部分,而不是将所有数据复制到临时工作表。无论队列中有多少张纸,它始终是最后一张纸。然后,该脚本将填写最终工作表的信息,但由于导出已经运行,因此信息不会进入PDF。

在脚本停止工作之前,我没有改变任何关于脚本的内容。由于它停止工作,我已经在故障排除中清理了一些东西,但没有改变行为。我尝试过使用lock()函数,但这并没有解决问题。非常感谢任何帮助,如果我能提供任何澄清信息,请告诉我。

(另外,忽略估计表上的总数错误,分配给该单元格的最小值。)

以下是脚本现在生成的输出:Current output

这是预期的输出(使用脚本生成,但通过打印到PDF手动导出):Manuall Export

function exportMultipleFullSheets() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var hopper = ss.getSheetByName("Sheet Export Setup");
  var cover = ss.getSheetByName("Cover Sheet");
  var namesToProcess = hopper.getRange(2, 1, 10, 1).getValues()
  var sheetsToProcess = new Array(10);
  Logger.log(sheetsToProcess);
  //Get the first of the sheets to be processed, which is where the cover sheet data is mostly pulled from.
  var ts = ss.getSheetByName(namesToProcess[0][0]);

  var producer = ts.getRange("K31").getValue();
  var contact = ts.getRange("K5").getValue();
  var showName = ts.getRange("F1").getValue();

  //Set up the temporary sheet, which will be exported then deleted
  var tempSheet = ss.insertSheet(showName + " Cost Estimate", 5, {
    template: cover
  });
  
  //Format column widths to correct size for printing
  var cw = 94; //Column Width

  tempSheet.setColumnWidth(1, cw);
  tempSheet.setColumnWidth(2, cw);
  tempSheet.setColumnWidth(3, cw);
  tempSheet.setColumnWidth(4, cw);
  tempSheet.setColumnWidth(5, cw);
  tempSheet.setColumnWidth(6, cw);
  tempSheet.setColumnWidth(7, cw);
  tempSheet.setRowHeight(18, 10);
  tempSheet.setRowHeight(18, 1);
  tempSheet.setRowHeight(32, 1);

  tempSheet.setRowHeight(1, 20);

  //Initialize the arrays
  var numCreated = 0;
  var dates = ["", "", "", "", "", "", "", "", "", ""];
  var guarantees = ["", "", "", "", "", "", "", "", "", ""];
  var descriptions = ["", "", "", "", "", "", "", "", "", ""];

  //For each sheet entered in the setup page, grab the relevant data and store it into the correct array. 
  var NTPIO;
  for (var i = 9; i > -1; i--) {
    NTPIO = ss.getSheetByName(namesToProcess[i][0]);
    if (NTPIO != null) {
      dates[i] = NTPIO.getRange("B1").getValue();
      guarantees[i] = NTPIO.getRange("I37").getValue();
      descriptions[i] = NTPIO.getRange("N31").getValue();

      tempSheet.insertColumnsAfter(7, 10);

      Logger.log(namesToProcess[i][0]);

      //resize the newly added columns to be correct for printing
      tempSheet.setColumnWidth(8, 15);
      tempSheet.setColumnWidth(9, 80);
      tempSheet.setColumnWidth(10, 80);
      tempSheet.setColumnWidth(11, 80);
      tempSheet.setColumnWidth(12, 80);
      tempSheet.setColumnWidth(13, 80);
      tempSheet.setColumnWidth(14, 80);
      tempSheet.setColumnWidth(15, 80);
      tempSheet.setColumnWidth(16, 80);
      tempSheet.setColumnWidth(17, 15);
      NTPIO.getRange("A1:J37").copyTo(tempSheet.getRange("H1:Q37"), {
        formatOnly: true
      });
      NTPIO.getRange("A1:J37").copyTo(tempSheet.getRange("H1:Q37"), {
        contentsOnly: true
      });
      numCreated++;
    }
  }

  tempSheet.getRange("C8").setValue(producer);
  tempSheet.getRange("C9").setValue(contact);
  tempSheet.getRange("C11").setValue(showName);

  //Copy the data from the arrays into the relevant cells in the temporary sheet.
  for (var i = 0; i < 9; i++) {
    tempSheet.getRange(15 + i, 2).setValue(dates[i]);
    tempSheet.getRange(15 + i, 4).setValue(guarantees[i]);
    tempSheet.getRange(15 + i, 5).setValue(descriptions[i]);
  }
  tempSheet.getRange("D15:D24").setNumberFormat("$0.00");

  //Final Height Adjustments
  tempSheet.setRowHeight(2, 10);
  tempSheet.setRowHeight(32, 1);
  tempSheet.setRowHeight(1, 20);


  var url = ss.getUrl();
  url = url.replace(/edit$/, '');
  var url_ext = 'export?exportFormat=pdf&format=pdf' // export as pdf
    +
    '&size=letter' // paper size
    +
    '&portrait=true' // orientation, false for landscape
    +
    '&fitw=false' // fit to width, false for actual size     
    +
    '&sheetnames=false&printtitle=false' // hide optional headers and footers
    +
    '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
    +
    '&fzr=false' // do not repeat row headers (frozen rows) on each page
    +
    '&gid='; // the sheet's Id

  var token = ScriptApp.getOAuthToken();

  var sheetname = tempSheet.getName();
  var date = sheetname.slice(0, sheetname.indexOf(" "));

  //make an empty array to hold your fetched blobs  
  var blobs = [];

  Logger.log(url + url_ext + tempSheet.getSheetId());

  var response = UrlFetchApp.fetch(url + url_ext + tempSheet.getSheetId(), {
    headers: {
      'Authorization': 'Bearer ' + token
    }
  });
  var output = response.getBlob().setName(sheetname + '.pdf');

  //Add file to the drive app. 
  var file = DriveApp.createFile(response).setName(Utilities.formatDate(dates[0], "(GMT-05:00)", "MM/dd/yyyy") + " " + sheetname);

  var folder = DriveApp.getFolderById("FOLDER ID");
  folder.addFile(file);

  //Clean up after you're done.
  ss.deleteSheet(tempSheet);
}

0 个答案:

没有答案