简体   繁体   English

在Google Apps脚本中使用Javascript

[英]Using Javascript in Google Apps Script

I'm trying to create a simple input-output webapp and have been having the hardest time getting anything to work. 我正在尝试创建一个简单的输入输出Web应用程序,并且一直很难完成任何工作。 The idea is to get user input from a textarea, add some simple html tags to it, and output it into another textarea. 想法是从文本区域获取用户输入,向其中添加一些简单的html标记,然后将其输出到另一个文本区域。 This is only to help some coworkers streamline their operations. 这只是为了帮助一些同事简化他们的操作。

I settled on making a standalone Google Apps Script webapp, and using the HTML service to create my UI. 我决定制作一个独立的Google Apps Script网络应用程序,并使用HTML服务创建用户界面。 The plan is to run Javascript in that HTML. 计划是在该HTML中运行Javascript。

I've tested the following code on W3schools' TryIt Editor, and it works fine. 我已经在W3schools的TryIt编辑器上测试了以下代码,并且工作正常。 It even works when I'm running it on StackOverflow's "run code snippet." 当我在StackOverflow的“运行代码段”上运行它时,它甚至可以工作。 But it won't function when I test my webapp. 但是当我测试我的web应用程序时,它将无法运行。

Help? 救命?

 <!DOCTYPE html> <html> <body> <br> <textarea rows=12 cols= 50 align=left id="input" placeholder='Insert text here...'></textarea> <input type="button" value="Try It" onclick="myFunction()" /> <textarea rows=12 cols= 50 align=right id="output" placeholder='Copy this text...' readonly></textarea> </body> <script type="text/JavaScript"> <!-- function myFunction() { var x = document.getElementById("input").value; document.getElementById("output").innerHTML = x; } --> </script> </html> 

Well the solution/problem is simple. 那么解决方案/问题很简单。 Your attribute values like cols = 50 needs to be in inverted commas. 您的cols = 50之类的属性值必须以逗号分隔。 Like so: 像这样:

cols ="50"

your Modified Code: 您的修改代码:

 <!DOCTYPE html> <html> <body> <br> <textarea rows="12" cols= "50" align="left" id="input" placeholder='Insert text here...'></textarea> <input type="button" value="Try It" onclick="myFunction()" /> <textarea rows="12" cols= "50" align="right" id="output" placeholder='Copy this text...' readonly></textarea> </body> <script type="text/JavaScript"> <!-- function myFunction() { var x = document.getElementById("input").value; document.getElementById("output").innerHTML = x; } --> </script> </html> 

The reason this works and the previous one doesnt work is the way google HTMLservice evaluates the HTML code. 这项工作有效且上一个无效的原因是Google HTMLservice评估HTML代码的方式。 Using your HTML code it evaluates your text area as this: 使用您的HTML代码,可以按以下方式评估您的文本区域:

<textarea rows="12 cols= 50 align=left id="input" placeholder='Insert text here...'></textarea>

which as you can see sets the row attribute to 如您所见,将row属性设置为

"12 cols =50 align=left id=" “ 12 cols = 50 align = left id =”

Hence when you run your function it is unable to find an element with id = "input" and throws an error in the console. 因此,当您运行函数时,无法找到id =“ input”的元素,并在控制台中引发错误。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM