App Script 未更新 Doc 以包含有关表单编辑的现有数据

App Script not updating Doc to include existing data on Form edits

我一直致力于根据 Google 表单提交自动填充表单的过程。到目前为止,我所有的进步都是我在这个网站上学到的。感谢大家的帮助!

假设表格已完整填写,我已让所有内容都按照 运行 所需的方式正常工作。技术人员填写 google 表格并提交。 he/she 提交后,脚本会自动生成包含所有数据的自定义文档。

运行一段时间后,技术人员要求我们在提交后将 Google 表单设置为可编辑。我做了一些研究,发现您可以使表单可编辑并编写一些脚本来为每个表单自动生成自定义 URL。

现在我运行在提交部分表单时遇到自动文档问题。如果技术人员填写表格的前半部分并提交,则会自动生成一份仅包含前半部分数据的文档,这是我所期望的。现在,如果他们返回并填写表格的第二部分,则只会填写文档的第二部分。我以为它会从 Sheet 中提取整行数据,但事实并非如此。

想了想,我想我需要重写代码以提交表单时自动生成Doc,因为越来越多的表单将被分段填写, 并在稍后的日期完成。

这是我想做的。任何见解或有用的链接将不胜感激!非常感谢!

所有表单数据当前都转到 Sheet,选项卡 A。我会在 sheet 上有第二个选项卡 B,名为 "Data to Doc"。我会手动梳理选项卡 A,然后将数据复制并粘贴到我想为其创建文档的选项卡 B。在 Sheet B 上,会有一个按钮,我可以单击该按钮 运行 脚本将 sheet 数据转换为 Doc 文件。

我找到了一个我想做的视频,但我似乎无法让我的代码按照他们的方式工作。 https://www.youtube.com/watch?v=r9uU_KwGgzQ

下面是我当前的代码。

var docTemplate = "fdgfdg"; 
var docName = "Technician Report";
var folder = DriveApp.getFolderById('hjhjghhgjgh');

function onFormSubmit(e) {
var replaceTextToImage = function(body, searchText, fileId) {
 var width = 300; // Please set this.
 var blob = DriveApp.getFileById(fileId).getBlob();
 var r = body.findText(searchText).getElement();
 r.asText().setText("");
 var img = r.getParent().asParagraph().insertInlineImage(0, blob);
 var w = img.getWidth();
 var h = img.getHeight();
 img.setWidth(width);
 img.setHeight(width * h / w);
}

//Get information from form and set as variables
var Technician = e.values[1];
var Customer_Name = e.values[2];
var Date = e.values[3];
var Facility_Location = e.values[4];
var WO_Project_No = e.values[5];
var PO_No = e.values[6];
var Tag_No = e.values[7];
var Site_Contact = e.values[8];
var Repair_Scope = e.values[9];
var Valve_Serial_No = e.values[10];
var Valve_Model = e.values[11];
var Valve_Condition = e.values[12];
var Valve_Action = e.values[13];
var Act_Serial_No = e.values[14];
var Act_Model = e.values[15];
var Act_Condition = e.values[16];
var Act_Action = e.values[17];
var Cont_Serial_No = e.values[18];
var Cont_Model = e.values[19];
var Cont_Condition = e.values[20];
var Cont_Action = e.values[21];
var Recommended_Actions = e.values[30];
var Call_Notes = e.values[32];
var Picture1_Notes = e.values[23];
var Picture2_Notes = e.values[25];
var Picture3_Notes = e.values[27];
var Picture1_Image = e.values[22].split("=")[1];
var Picture2_Image = e.values[24].split("=")[1];
var Picture3_Image = e.values[26].split("=")[1];



// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+Customer_Name+' '+Tag_No, folder)
.getId();  
 // Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getBody();


copyBody.replaceText('A1', Technician);
copyBody.replaceText('A2', Customer_Name);
copyBody.replaceText('A3', Date);
copyBody.replaceText('A4', Facility_Location);
copyBody.replaceText('A5', WO_Project_No);
copyBody.replaceText('A6', PO_No);
copyBody.replaceText('A7', Tag_No);
copyBody.replaceText('A8', Site_Contact);
copyBody.replaceText('A9', Repair_Scope);
copyBody.replaceText('B1', Valve_Serial_No);
copyBody.replaceText('B2', Valve_Model);
copyBody.replaceText('B3', Valve_Condition);
copyBody.replaceText('B4', Valve_Action);
copyBody.replaceText('B5', Act_Serial_No);
copyBody.replaceText('B6', Act_Model);
copyBody.replaceText('B7', Act_Condition);
copyBody.replaceText('B8', Act_Action);
copyBody.replaceText('B9', Cont_Serial_No);
copyBody.replaceText('C1', Cont_Model);
copyBody.replaceText('C2', Cont_Condition);
copyBody.replaceText('C3', Cont_Action);
copyBody.replaceText('C4', Recommended_Actions);
copyBody.replaceText('C5', Call_Notes);
copyBody.replaceText('C8', Picture1_Notes);
copyBody.replaceText('D1', Picture2_Notes);
copyBody.replaceText('D3', Picture3_Notes);
replaceTextToImage(copyBody, 'C7', Picture1_Image);
replaceTextToImage(copyBody, 'C9', Picture2_Image);
replaceTextToImage(copyBody, 'D2', Picture3_Image);


copyDoc.saveAndClose();

假设您使用 onSubmit 触发器将脚本绑定到您的表单,您可以检索最新响应的长度并将其与问题总数进行比较:

function myFunction(e) {
  var length = FormApp.getActiveForm().getItems().length;
    if(e.response.getItemResponses().length == length){
    //proceed to generate custom document
  }else{   
    Logger.log("Form is not complete");
  }
}

更新:如果选中复选框,则替代解决方案创建文档

  • 在这种情况下,您不希望 运行 在提交表单时使用该功能,而是在对复选框进行编辑时使用。

  • 所以,通过onEdit触发器替换onSubmit触发器。

  • 这将允许您访问事件对象 e.rangee.oldValuee.value

  • 您可以实施 if 语句,仅当在复选框列中进行了编辑并且旧值为 false (unchecked) and the new one istrue`(选中).
  • 很遗憾,您无法使用 e.values 检索表单响应 onEdit,相反,您可以验证在哪一行中选中了复选框,并使用 sheet.getRange(row, 1, 1, sheet.getLastColumn()).getValues(); 检索该行的值
  • 要实施更改,请创建一个带有复选框的列(假设在第 29 列中,因为之前的列已被响应占用)并修改您的代码如下:
function onEdit(e){
  if(e.range.getColumn() == 29 || e.oldValue == false && e.value == true){
    var row = e.range.getRow();
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var values = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getValues()[0];
    var replaceTextToImage = function(body, searchText, fileId) {
      var width = 300; // Please set this.
      var blob = DriveApp.getFileById(fileId).getBlob();
      var r = body.findText(searchText).getElement();
      r.asText().setText("");
      var img = r.getParent().asParagraph().insertInlineImage(0, blob);
      var w = img.getWidth();
      var h = img.getHeight();
      img.setWidth(width);
      img.setHeight(width * h / w);
    }

    //Get information from form and set as variables
    var Technician = values[1];
    var Customer_Name = values[2];
   ...
   ... 
    copyDoc.saveAndClose();
  }
}