【问题标题】:Adding editor with script throws 'Invalid email' and stops script添加带有脚本的编辑器会引发“无效电子邮件”并停止脚本
【发布时间】:2020-10-10 20:57:49
【问题描述】:

我正在使用 Apps 脚本生成一个包含来自 Google 表单的响应的文档,并将其存储在一个新文件夹中。我还允许用户根据他们提供的电子邮件提交表单编辑器访问权限。

.addEditor(email);

这似乎适用于使用 g-suite 的 Google 域或公司域。

但是,如果电子邮件不是基于 Google 的,则脚本会中断。

'Invalid email: email@example.com'

寻找跳过此错误并完成脚本的方法。

  function autoFillGoogleDocFromForm(e) {
  var Timestamp = e.values[0];
  var email = e.values[1];

  var file = DriveApp.getFileById('FILEID');
  var folder = createfolder(); 
  var copy = file.makeCopy(Name + ' - Document', folder); 
  var newId = copy.getId();
  var doc = DocumentApp.openById(newId).addEditor(email);
  
  var body = doc.getBody();
  body.replaceText('{{Timestamp}}', Timestamp); 
  body.replaceText('{{Email}}', Email);  
  doc.saveAndClose();                                             


}

【问题讨论】:

    标签: google-apps-script google-docs google-forms google-workspace


    【解决方案1】:

    寻找跳过此错误并拥有脚本的方法 完成。

    如果您正在寻找跳过错误的解决方案,那么您可以随时使用try...catch

      function autoFillGoogleDocFromForm(e) {
      var Timestamp = e.values[0];
      var email = e.values[1];
    
      var file = DriveApp.getFileById('FILEID');
      var folder = createfolder(); 
      var copy = file.makeCopy(Name + ' - Document', folder); 
      var newId = copy.getId();
      
      try{
      var doc = DocumentApp.openById(newId).addEditor(email);
      }
      catch(error){
      var doc = DocumentApp.openById(newId);
      }
      
      var body = doc.getBody();
      body.replaceText('{{Timestamp}}', Timestamp); 
      body.replaceText('{{Email}}', Email);  
      doc.saveAndClose();                                             
      
    }
    

    这个sn-p将try执行var doc = DocumentApp.openById(newId).addEditor(email);

    如果后者失败,那么它只会打开文档var doc = DocumentApp.openById(newId); 并继续执行其余代码。

    【讨论】:

    • 谢谢马里奥斯,我确实希望其余的代码继续执行,所以如果电子邮件出现错误,它会继续完成替换文本功能
    • @EwanFarry 感谢您的反馈。请尝试我更新的解决方案。
    猜你喜欢
    • 2020-06-17
    • 2012-03-21
    • 1970-01-01
    • 2010-11-22
    • 2023-01-20
    • 2019-01-20
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多