2017-10-11 26 views
1

我试图从谷歌应用脚​​本访问p5.js库。下面是我尝试过的doGet()函数。我做错了吗?或者,当使用应用程序脚本时,此库无法访问?是否有可能在谷歌应用脚​​本中使用p5.js库

function doGet() { eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.js', Object).getContentText()); 

return HtmlService.createHtmlOutputFromFile('index'); 
} 

我也试着通过去除对象参数这样更新代码:

function doGet() { 
eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.js').getContentText()); 
     return HtmlService.createHtmlOutputFromFile('index'); 
    } 

但我仍然得到一个错误。

SyntaxError:之后缺少名称。运营商。

更新: 我从下面试过马里奥的代码。它修复了第一个错误。但现在我正在尝试创建一个画布,而我所看到的只是一个空白页面。没有错误列出。以下是代码现在的样子。

doget.gs

function doGet() { 
    var template = HtmlService.createTemplateFromFile("demo"); 
    return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME); 
} 

sketch.gs

function setup() { 
    createCanvas(400,600); 
} 

function draw() { 
    background(100); 
} 

demo.html

<!DOCTYPE html> 
<html> 
<head> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.js"></script> 
    <script src="sketch.gs"></script> 
</head> 
<body> 
</body> 
</html> 
+0

执行此操作时,会发生什么的谷歌文档?你有错误信息吗? –

+0

它说,“不好的价值” – GMath314

回答

1

首先,必须创建一个新的文件,并把你所有的HTML代码的JavaScript你想用。

demo.html

<html> 
<head> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.js"></script> 
    <?!= include('sketch'); ?> 
</head> 
<body> 
</body> 
</html> 

sketch.html

<script> 
function setup() { 
    createCanvas(400,600); 
} 

function draw() { 
    background(100); 
} 
</script> 
在Appsscript文件code.gs.

然后这个创建的谷歌Apps脚本代码和HTML混合制作动态网页

function doGet() { 
    var template = HtmlService.createTemplateFromFile("demo"); 
    return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME); 
} 

function include(filename) { 
    return HtmlService.createHtmlOutputFromFile(filename) 
     .getContent(); 
} 

这里是HTML服务 https://developers.google.com/apps-script/guides/html/templates

+2

请解释这段代码的作用,以及它为什么是正确的答案,而不是仅仅倾销不明原因的代码。 –

+0

马里奥的代码修复了第一个错误。但现在我只看到一个空白页面。没有画布。我更新了原始文章中的代码,以展示我迄今为止的内容。它不会返回任何错误。只有一个空白的屏幕。 – GMath314

+0

哇!这很有道理!谢谢,马里奥!你是男人! – GMath314

相关问题