【问题标题】:DriveApp conversion from Google Sheet to PDF now failsDriveApp 从 Google Sheet 到 PDF 的转换现在失败了
【发布时间】:2018-08-24 03:33:37
【问题描述】:

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

【问题讨论】:

  • 导出为 PDF 的电子表格文档是否填写正确?还是空白?
  • 电子表格填写正确
  • 当您将 PDF 的 "application/PDF" 更改为 MimeType 常量时会发生什么?即输入 MimeType. 并从自动完成选项中选择正确的值。
  • 嗨@tehhowch 我尝试将行更改为 var theBlob = pdf.getBlob().setName(inputArray[2]).getAs(MimeType.PDF);它会创建一个 PDF,但仍然是空的。不确定我的代码格式是否正确?我写了原始脚本,但我不是开发人员
  • 是的,这就是我的本意。看起来您也可以放弃第一个 getBlob 调用,即 pdf.getAs(MimeType.PDF)pdf.getBlob().getAs(MimeType.PDF) 相同。您是否在其他电子表格或仅此工作表上遇到同样的问题?最近是否有新内容添加到此电子表格中?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2022-06-14
    • 1970-01-01
    • 2022-12-30
    • 1970-01-01
    相关资源
    最近更新 更多