从Google Sheet到PDF的DriveApp转换现在失败

时间:2018-03-15 10:50:10

标签: google-apps-script google-sheets google-drive-api

要求慈善机构的志愿者填写申请表(谷歌表格)。当他们提交时,运行以下脚本。基本上,它将输入电子表格的最后一行复制到新的电子表格(使用具有所有正确格式的主电子表格的副本),然后将该新电子表格保存为PDF。

PDF保存突然停止工作(它已经运行了大约一年),现在只需创建一个空白的PDF。

有人可以解释一下我需要做些什么才能让它再次运作?

//This section copies the last completed row to a new spreadsheet, using a copy of the master sheet, saves as a PDF and cleans up     
//Create an input array to hold the last row data
    var inputArray = [];
//Copy the responses into an array
  for (i=1;i <=48;i++) {
    inputArray[i] = inputSheet.getRange(lastRowInput, i, 1, 1).getValue();
  }

//Get some info from the SysAdmin sheet which contains the IDs of the folder, master app form and demographics sheet  
var sysAdminSheet = spreadSheet.getSheetByName("SysAdmin")
var folderID = sysAdminSheet.getRange("B1").getValue();
var masterCopy = sysAdminSheet.getRange("B2").getValue();
var demoSheetID = sysAdminSheet.getRange("B3").getValue();
var nokSheetID = sysAdminSheet.getRange("B4").getValue();
var medSheetID = sysAdminSheet.getRange("B5").getValue();

//Because the data will be loaded into picklists we need to manipulate the answer to the disability q to fit.
  var tempDis = inputArray[47];
  switch (tempDis) {
    case "Specific learning disability (such as dyslexia or dyspraxia)":
      inputArray[47] = "Specific learning disability";
      break;
    case "General learning disability (such as Down's syndrome)":
      inputArray[47] = "General learning disability";
      break;
    case "Cognitive impairment (such as autistic spectrum disorder or resulting from head injury)":
      inputArray[47] = "Cognitive impairment";
      break;
    case "Long-standing illness or health condition (such as cancer, HIV, diabetes, chronic heart disease, or epilepsy)":
      inputArray[47] = "Long-standing illness or health condition";
      break;
    case "Mental health condition (such as depression or schizophrenia)":
      inputArray[47] = "Mental health condition";
      break;
    case "Physical impairment or mobility issues (such as difficulty using arms or using a wheelchair or crutches)":
      inputArray[47] = "Physical impairment or mobility issues";
      break;
  }


//Copy the spreadsheet containing the master form into a new spreadsheet, name it with the email address and get it's id
//inputArray[2] contains the email address, which is used as the spreadsheet name
var destFolder = DriveApp.getFolderById(folderID);  
var newSheet = DriveApp.getFileById(masterCopy).makeCopy(inputArray[2], destFolder).getId();

// Open the new sheet and assign it to the outputSheet var
var outputSheet = SpreadsheetApp.openById(newSheet);
//Copy the info into the outputsheet. Because it has 'section headers' (See row 18 for an example) we can't just do a straight copy
  for (i=2;i<=17;i++) {
    outputSheet.getRange("B" + i).setValue(inputArray[i]);
  }
//Referee info  
  for (i=19; i<=31;i++) {
    outputSheet.getRange("B" + i).setValue(inputArray[i-1]);
  }
//NoK info
  for (i=32;i<=41;i++) {
    outputSheet.getRange("B" + i).setValue(inputArray[i-2])
  }
//Confidentiality question is the only one in this section
  outputSheet.getRange("B42").setValue(inputArray[39]);
//As above for the rehab of offenders question
  outputSheet.getRange("B44").setValue(inputArray[40]);
//And the Equal opps questions
  for (i=46;i<=53;i++) {
    outputSheet.getRange("B" + i).setValue(inputArray[i-5]);
  }
//This is the bit that isn't working
//Save the file as a PDF

var pdf = DriveApp.getFileById(outputSheet.getId());
var theBlob = pdf.getBlob().getAs('application/pdf').setName(inputArray[2]);
destFolder.createFile(theBlob);

1 个答案:

答案 0 :(得分:0)

通过添加SpreadsheetApp.flush()解决它; PDF创建之前的命令,以确保提交所有电子表格更改。