从Google Form转换为doc

时间:2019-03-07 23:38:37

标签: google-sheets google-docs google-sheets-api google-form google-docs-api

好的开发人员,我的英语不是很好,请多多包涵。 我是一名学生,所以我第一次接触了Google Script表格。 我有一种情况:

Step.1用户填写Google表单

Step.2 Google表单将数据传输到脚本

Step.3 Google表单还将数据发送到Google表格

Step.4脚本将使用表单数据生成文档,并从模板文档复制文档

Step.5 Google表格中的列值可以链接到文档

在阅读了Internet上的很多文章之后,我认为这很容易,我尝试了以下代码,“偶尔成功了”:

function myFunction(e) {
  const folderID = '##';
  const templateFile = '##';
  const sheetPath = '##';
  const nameCol = 4;

  const arr = ['timestamp', 'email', 'Company Name', 'Contact Man'];

  const ss = SpreadsheetApp.openById(sheetPath);
  const sheet = ss.getSheets()[0]; 
  Logger.log('nesl_timestamp:' + e.namedValues);
  const timestamp = e.namedValues['timestamp']; //0
  const email = e.namedValues['email']; //1
  const ein = e.namedValues['EIN / Uniform Number']; //2
  const name = e.namedValues['Company Name']; //3
  const man = e.namedValues['Contact Man']; //4
  const tel = e.namedValues['TEL']; //5
  const add = e.namedValues['adress']; //6
  const title = e.namedValues['Title']; //7
  const job = e.namedValues['Job content']; //8
  const time = e.namedValues['Working hours']; //9
  const type = e.namedValues['Salary or Wage ']; //10
  const salary = e.namedValues['Salary']; //11
  const dep1 = e.namedValues['Department [1]']; //12
  const dep2 = e.namedValues['Department [2]']; //13

  const other = e.namedValues['Other Inf.']; //15
  const method = e.namedValues['Application Method']; //16
  const data = e.namedValues['Prepare data']; //17
  var userfile = e.namedValues['userfile']; //18
  const exp = e.namedValues['exp']; //19
  const people = e.namedValues['people ']; //20
  const addno = e.namedValues['addno']; //21
  const otherThing = e.namedValues['otherThing ']; //22

  var arr2 = [timestamp, email, name, man];

  //todo:set userfile path
  if (userfile.length != 0 && userfile.indexOf(',') > -1) userfile = userfile.split(',')[0];
  Logger.log('nesl_userfile:' + userfile);

  //todo: copy template.doc to doc
  const copy = DriveApp.getFileById(templateFile).makeCopy(name + ',' + timestamp, DriveApp.getFolderById(folderID));
  const docID = copy.getId();
  Logger.log('nesl_docID:' + docID);

  const doc = DocumentApp.openById(docID);
  var body = doc.getBody();

  body.replaceText('{{timestamp}}', timestamp);
  body.replaceText('{{ein}}', ein);
  body.replaceText('{{name}}', name);
  body.replaceText('{{man}}', man);
  body.replaceText('{{tel}}', tel);
  body.replaceText('{{add}}', add);
  body.replaceText('{{title}}', title);
  body.replaceText('{{job}}', job);
  body.replaceText('{{time}}', time);
  body.replaceText('{{type}}', type);
  body.replaceText('{{salary}}', salary);
  body.replaceText('{{dep1}}', dep1);
  body.replaceText('{{dep2}}', dep2);
  body.replaceText('{{other}}', other);
  body.replaceText('{{method}}', method);
  body.replaceText('{{data}}', data);
  body.replaceText('{{exp}}', exp);
  body.replaceText('{{people}}', people);
  body.replaceText('{{addno}}', addno);
  body.replaceText('{{otherThing}}', otherThing);
  body.replaceText('{{userfile}}', userfile);

  doc.saveAndClose();

  //todo: get doc id insert into sheet
  //I think this may have to be written after openById(), otherwise he will copy the file twice, but I find that it will copy the file twice when I first add the data. I am not sure about myself now.
  sheet.getRange(sheet.getLastRow(), sheet.getLastColumn()).setValue(docID);
  Logger.log('nesl_update:' + sheet.getLastRow() + ',' + sheet.getLastColumn());

  //todo: set name column link
  //I think this may have to be written after openById(), otherwise he will copy the file twice, but I find that it will copy the file twice when I first add the data. I am not sure about myself now.
  var nameColumnSheet = sheet.getRange(sheet.getLastRow(), nameCol);
  sheet.getRange(sheet.getLastRow(), nameCol).setValue(
    '=HYPERLINK("https://docs.google.com/document/d/' + docID + '","' + nameColumnSheet.getValue() + '");'
  );

  //todo:Send the link to the user in the email
  var html =
    '<body>' +
    '<h2> thank: ' + man + ' </h2><br/>' +
    '<p> link:' + 'https://docs.google.com/document/d/' + docID + '<br/> and http://dce.ntub.edu.tw/p/404-1029-67423.php?Lang=zh-tw </p>' +
    '</body>';

  MailApp.sendEmail(
    email,
    'nnboss@ntub.edu.tw',
    'ntub',
    { htmlBody: html }
  );
}

它通常具有以下错误:

  • 将相同数据多次添加到工作表中。
  • 复制多个模板文档,将文档{{item}}替换为“空白”,成为空白文档
  • 但是空白文档将保留时间戳
  • 工作表中的链接将链接到空白文档
  • 但是,电子邮件中的链接已链接到正确的文档,并且不会重复发送。
  • 链接消失
  • 其他错误

这些错误通常在我填写表格时打开工作表或执行压力测试时发生。 我只能安慰自己,也许我不应该打开床单,但这很奇怪。他的错误仍然会出现。

在我看来,这好像多次调用了myFunction(),第二次没有'e',但它应该出现'ReferenceError:'e'is not defined',并且没有理由这样做。

这个问题非常复杂,我的英语不好,我什至不知道如何获取关键字。

请帮助我。

template doc

Correct doc

blank doc

如果有人需要尝试:

文件夹: https://drive.google.com/open?id=1lv7euKTQcS5Ooa7UUErRc3eBJNZz01jx

表格: https://docs.google.com/forms/d/16yzqi0ClVUBokXmehDAzY0otY7TdN8WTiBY2zHNXD7g

页: https://docs.google.com/spreadsheets/d/1oAvIpx02f2eSqZl7jHpNsh2AAw31H8RGNw50W38lmDk

模板文档: https://docs.google.com/document/d/11QGRwplzVNtjcYa_n64HbQ6OwCiJxsBkWergxAvd0Ho

脚本代码: https://docs.google.com/document/d/1sKFzgz6PKzuIXL3qP2E1CntFRvooxI0tY6K4D1Ksj6c

0 个答案:

没有答案