javascript 参考错误:“文档”未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13552250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:08:06  来源:igfitidea点击:

ReferenceError: "document" is not defined

javascriptgoogle-apps-scriptgoogle-sitesreferenceerror

提问by Bruno Estrazulas

Im new to JavaScript and even more new to Google Apps Script. Im trying a simple function that shows the current date (only day, month e and full year), but the Google Script show the error ReferenceError: "document" is not defined.

我是 JavaScript 的新手,对 Google Apps 脚本更陌生。我正在尝试一个显示当前日期(仅日、月和年)的简单函数,但 Google 脚本显示错误 ReferenceError: "document" is not defined。

My goal is to use this function in a Google Site. Here is the code:

我的目标是在 Google 站点中使用此功能。这是代码:

function Data()
{
var d=new Date();
var dia=d.getDate();
var mes=d.getMonth();
var ano=d.getFullYear();
var DataCompleta=dia + "/" + mes + "/" + ano
document.write(DataCompleta);
}

采纳答案by Serge insas

As said in the former answer you can't execute a function directly in the Browser, you'll have to choose a so called 'container' to run your function from it. I would recommand you read the documentationand maybe try a few simple tutorialsto see how GAS can be executed.

正如前一个答案中所说,您不能直接在浏览器中执行函数,您必须选择一个所谓的“容器”来运行您的函数。我建议您阅读文档,也许可以尝试一些简单的教程来了解如何执行 GAS。



EDIT :following your comments, feel free to have a look at this scriptbuilt with UiApp, the result is viewable hereand shows what you wanted to : "Hello, today is 25/10/2012"

编辑:根据您的评论,请随意查看使用 UiApp 构建的脚本,结果可在此处查看并显示您想要的内容:“您好,今天是 25/10/2012”

回答by Quentin

Code running as a Google Apps Script does not run in the browser, so you cannot use web browser APIs with it. If you want to output content to a Google Site, then you need to use the API for Sites.

作为 Google Apps 脚本运行的代码不会在浏览器中运行,因此您不能将网络浏览器 API 与它一起使用。如果要将内容输出到 Google 站点,则需要使用站点 API

Presumably you would want something like createWebPageand then use the methods on the resulting objectto add the content to it.

大概你会想要类似的东西createWebPage,然后使用结果对象上的方法向它添加内容。