函数未在Google脚本HTML服务中执行 [英] Function not executing in google script HTML service

查看:40
本文介绍了函数未在Google脚本HTML服务中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我自己和我的同学编写一个工具,该工具将创建Google文档并将其通过电子邮件发送给选定的老师,并使用所有填写的字段并将所有不包含选定主题默认值的字段填满,例如语言艺术,但是接受所选信息并将其用于发送电子邮件的功能未执行.我已经检查了项目的执行情况,并且函数customDoc()尚未执行.我怀疑这是我的HTML的问题,因为当我在编辑器中测试该函数以查看是否存在语法错误时,我没有看到任何错误消息,但是它很干净,只是从未执行过.这是我的代码,虽然HTMl中可能会出现错误,但我也会提供我的JS.

 function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index.html');
}

function showDialoge() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutputFromFile('Index.html'), 'Test');
}

function customDoc(clicked_id) {
  var d = new Date();
  var s = (d.getDate()) + '/' + (d.getMonth() + 1) + '/' + d.getFullYear();
  console.log(s);
  var cycler = clicked_id
  var math = ['esmith3@op97.org', 'math for ']
  var LA = ['cborah@op97.org', 'la for ']
  var science = ['lgrimaldi@op97.org', 'science for ']
  var is = ['charrington@op97.org', 'I&S for ']
  var span = ['vlagioia@op97.org', 'Espanol para ']
  var presets = [math, LA, science, is, span]
  var email1 = document.getElementById('Email')
  var subject1 = document.getElementById('Sub')
  var docName1 = document.getElementById('docName')
  var message1 = document.getElementById('message')
  var email = null
  if (email1 != ' ') {
    email = email1
  } else {
    email = presets[cycler];
    [1];
  }
  var subject = null
  if (subject1 != ' ') {
    subject = subject1
  } else {
    subject = presets[cycler];
    [2]; + s
  }
  var doc = null
  if (docName1 != ' ') {
    doc = docName1
  } else {
    doc = presets[cycler];
    [2]; + s
  }
  var document = documentApp.create(doc)
  var url = document.getUrl();
  var message = null
  if (message1 != ' ') {
    message = message1 + '' + url
  } else {
    message = url
  }

  GmailApp.sendEmail(email, subject, message);
}
 

 <!DOCTYPE html>
<script src="Code.gs"></script>
<html>
<h1>CREATE DOC</h1>

<body>


</body>
<p>Email</p>
<input type='text' id='Email' value=' ' style="border-radius: 20px; border-color: crimson; border-width:20px; ">
<p style=" font-family: Times New Roman, Times, serif;">Doc name</p>
<input type='text' id='docName' value=' ' style="border-radius: 20px; border-color: crimson; border-width:20px; ">
<p>Subject</p>
<input type='text' id='Sub' value=' ' style="border-radius: 20px; border-color: crimson; border-width:20px; ">
<p>message</p>
<input type='text' id='message' value=' ' style="border-radius: 20px; border-color: crimson; border-width:20px; ">

<h2>Fill blanks for subject: </h2>


<button id='2' onclick=c ustomDoc(this.id)> LA </button>
<button id='3' onclick=c ustomDoc(this.id)> Science </button>
<button id='4' onclick=c ustomDoc(this.id)> Individuals and societies </button>
<button id='5' onclick=c ustomDoc(this.id)> Spanish  </button>
<button id='1' onclick=c ustomDoc(this.id)> math </button>


</html>
 

解决方案

简而言之,customDoc()是服务器功能,您需要使用google.script.run告诉Apps脚本运行特定的服务器功能.因此,请尝试使用onclick="google.script.run.customDoc(this.id)",而不是调用onclick="customDoc(this.id)".不要在您的HTML文件中包含Code.gs,因为这是服务器端代码,因此无法正常工作.我强烈建议您阅读客户端到服务器的通讯指南. /p>

您的customDoc()函数是另一个故事:)下面是使用 ,可能更容易理解.

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index.html');
}

function customDoc(subject) {
  var subjects = {
    'math': {
      email: 'teacher@example.com',
      preSubject: 'math for '
    },
    'la': {
      email: 'teacher@example.com',
      preSubject: 'la for '
    },
    'science': {
      email: 'teacher@example.com',
      preSubject: 'science for '
    },
    'is': {
      email: 'teacher@example.com',
      preSubject: 'I&S for '
    },
    'spanish': {
      email: 'teacher@example.com',
      preSubject: 'Español para '
    }
  };

  var formattedDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'M/d/yyyy');
  
  console.log('Today: ' + formattedDate);
  console.log('Subject: ' + subject);
  console.log(subjects[subject]);
}

<!DOCTYPE html>
<html>
  <body>
    <h1>CREATE DOC</h1>
    <p>Email</p>
    <input type="text" id="Email" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />
    
    <p style="font-family: Times New Roman, Times, serif">Doc name</p>
    <input type="text"  id="docName" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />
    
    <p>Subject</p>
    <input type="text" id="Sub" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />
    
    <p>message</p>
    <input type="text" id="message" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />

    <h2>Fill blanks for subject:</h2>

    <button id="la" onclick="google.script.run.customDoc(this.id)">LA</button>
    <button id="science" onclick="google.script.run.customDoc(this.id)">Science</button>
    <button id="is" onclick="google.script.run.customDoc(this.id)">Individuals and societies</button>
    <button id="spanish" onclick="google.script.run.customDoc(this.id)">Spanish</button>
    <button id="math" onclick="google.script.run.customDoc(this.id)">math</button>
  </body>
</html>

尝试运行以上命令,然后单击科学"按钮.您应该获得如下执行日志:

 Today: 10/30/2020
Subject: science
{preSubject=science for , email=teacher@example.com}
 

现在实际上正在执行customDoc(),您可以开始尝试修复Google Doc的生成.在我看来,您正在创建一个完全空白的Google文档,可能不是您想要的.我认为您需要做更多的工作,如果对生成文档有更具体的问题,请回来.祝你好运!

I am coding a tool for myself and my classmates that will create a google doc and email it to the selected teacher, using all fields that are filled in and filling all those that are not with the defaults for the selected subject, say language arts, but the function that takes the selected information and uses it to send the email is not executing. I've checked the executions for the project, and the function, customDoc(), has not once been executed. I suspect it's a problem with my HTML, as I have not seen any error messages when I tested the function in the editor to see if there were any syntax errors, but it was spot clean, just never executed. Here's my code, and while the error is likely in the HTMl I will provide my JS too.

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index.html');
}

function showDialoge() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutputFromFile('Index.html'), 'Test');
}

function customDoc(clicked_id) {
  var d = new Date();
  var s = (d.getDate()) + '/' + (d.getMonth() + 1) + '/' + d.getFullYear();
  console.log(s);
  var cycler = clicked_id
  var math = ['esmith3@op97.org', 'math for ']
  var LA = ['cborah@op97.org', 'la for ']
  var science = ['lgrimaldi@op97.org', 'science for ']
  var is = ['charrington@op97.org', 'I&S for ']
  var span = ['vlagioia@op97.org', 'Espanol para ']
  var presets = [math, LA, science, is, span]
  var email1 = document.getElementById('Email')
  var subject1 = document.getElementById('Sub')
  var docName1 = document.getElementById('docName')
  var message1 = document.getElementById('message')
  var email = null
  if (email1 != ' ') {
    email = email1
  } else {
    email = presets[cycler];
    [1];
  }
  var subject = null
  if (subject1 != ' ') {
    subject = subject1
  } else {
    subject = presets[cycler];
    [2]; + s
  }
  var doc = null
  if (docName1 != ' ') {
    doc = docName1
  } else {
    doc = presets[cycler];
    [2]; + s
  }
  var document = documentApp.create(doc)
  var url = document.getUrl();
  var message = null
  if (message1 != ' ') {
    message = message1 + '' + url
  } else {
    message = url
  }

  GmailApp.sendEmail(email, subject, message);
}

<!DOCTYPE html>
<script src="Code.gs"></script>
<html>
<h1>CREATE DOC</h1>

<body>


</body>
<p>Email</p>
<input type='text' id='Email' value=' ' style="border-radius: 20px; border-color: crimson; border-width:20px; ">
<p style=" font-family: Times New Roman, Times, serif;">Doc name</p>
<input type='text' id='docName' value=' ' style="border-radius: 20px; border-color: crimson; border-width:20px; ">
<p>Subject</p>
<input type='text' id='Sub' value=' ' style="border-radius: 20px; border-color: crimson; border-width:20px; ">
<p>message</p>
<input type='text' id='message' value=' ' style="border-radius: 20px; border-color: crimson; border-width:20px; ">

<h2>Fill blanks for subject: </h2>


<button id='2' onclick=c ustomDoc(this.id)> LA </button>
<button id='3' onclick=c ustomDoc(this.id)> Science </button>
<button id='4' onclick=c ustomDoc(this.id)> Individuals and societies </button>
<button id='5' onclick=c ustomDoc(this.id)> Spanish  </button>
<button id='1' onclick=c ustomDoc(this.id)> math </button>


</html>

解决方案

In short, customDoc() is a server function and you need to use google.script.run to tell Apps Script to run a specific server function. So instead of calling onclick="customDoc(this.id)", try onclick="google.script.run.customDoc(this.id)". Don't include Code.gs in your HTML file as that's server-side code and it won't work. I highly recommend reading the Client-to-Server Communication guide.

Your customDoc() function is another story :) Below is a very simple way to reorganize your different presets (e.g. subjects) using objects. I also replaced your date formatting code with Utilities.formatDate(), which may be a little easier to comprehend.

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index.html');
}

function customDoc(subject) {
  var subjects = {
    'math': {
      email: 'teacher@example.com',
      preSubject: 'math for '
    },
    'la': {
      email: 'teacher@example.com',
      preSubject: 'la for '
    },
    'science': {
      email: 'teacher@example.com',
      preSubject: 'science for '
    },
    'is': {
      email: 'teacher@example.com',
      preSubject: 'I&S for '
    },
    'spanish': {
      email: 'teacher@example.com',
      preSubject: 'Español para '
    }
  };

  var formattedDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'M/d/yyyy');
  
  console.log('Today: ' + formattedDate);
  console.log('Subject: ' + subject);
  console.log(subjects[subject]);
}

<!DOCTYPE html>
<html>
  <body>
    <h1>CREATE DOC</h1>
    <p>Email</p>
    <input type="text" id="Email" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />
    
    <p style="font-family: Times New Roman, Times, serif">Doc name</p>
    <input type="text"  id="docName" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />
    
    <p>Subject</p>
    <input type="text" id="Sub" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />
    
    <p>message</p>
    <input type="text" id="message" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />

    <h2>Fill blanks for subject:</h2>

    <button id="la" onclick="google.script.run.customDoc(this.id)">LA</button>
    <button id="science" onclick="google.script.run.customDoc(this.id)">Science</button>
    <button id="is" onclick="google.script.run.customDoc(this.id)">Individuals and societies</button>
    <button id="spanish" onclick="google.script.run.customDoc(this.id)">Spanish</button>
    <button id="math" onclick="google.script.run.customDoc(this.id)">math</button>
  </body>
</html>

Try running the above and clicking the science button. You should get an execution log like:

Today: 10/30/2020
Subject: science
{preSubject=science for , email=teacher@example.com}

Now that customDoc() is actually executing, you can start trying to fix the Google Doc generation. It seems to me that you're creating a completely blank Google Doc, which probably isn't what you want. I think you need to work on it some more and then come back if you have more specific questions about generating the document. Good luck!

这篇关于函数未在Google脚本HTML服务中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆