Google Apps脚本:htmlservice.createhtmloutput在iframe中不起作用

发布于 2025-02-03 00:42:37 字数 2494 浏览 2 评论 0 原文

  • 创建一个空白的Google文档文件
  • 添加以下应用程序脚本:
function onOpen(e)
{
    DocumentApp.getUi().createAddonMenu().addItem('Open dialog', 'openDialog').addItem('Open html', 'openHtml').addToUi();
}
function openDialog() {
    DocumentApp.getUi().alert('This works');
}
function openHtml() {
    let output = HtmlService.createHtmlOutput('<b>This works</b>');
    output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
    DocumentApp.getUi().showModalDialog(output, 'Title');
}

现在出现这两个菜单条目:

”在此处输入图像描述“

”打开对话框

。 png“ rel =” nofollow noreferrer“> “

“ open html” works:

将Google Docs文件嵌入iframe:

<iframe
   src="https://docs.google.com/document/d/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/edit"
></iframe>

“打开对话框”工作:

“在此处输入图像说明”

“ open html”不起作用:

console中的错误消息:

< console:<<< a href =“ https://i.sstatic.net/qyc38.png” rel =“ nofollow noreferrer”>

我认为这正是这一行:

output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);

我缺少什么?

这是重现该问题的链接:

  • Create a blank google docs file
  • Add the following apps script:
function onOpen(e)
{
    DocumentApp.getUi().createAddonMenu().addItem('Open dialog', 'openDialog').addItem('Open html', 'openHtml').addToUi();
}
function openDialog() {
    DocumentApp.getUi().alert('This works');
}
function openHtml() {
    let output = HtmlService.createHtmlOutput('<b>This works</b>');
    output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
    DocumentApp.getUi().showModalDialog(output, 'Title');
}

Now these 2 menu entries appear:

enter image description here

"Open dialog" works:

enter image description here

"Open html" works:

enter image description here

Embed the google docs file in an iframe:

<iframe
   src="https://docs.google.com/document/d/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/edit"
></iframe>

"Open dialog" works:

enter image description here

"Open html" does not work:

enter image description here

Error message in console:

enter image description here

I thought this is exactly what this line does:

output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);

What am I missing?

Here is a link to reproduce that problem:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不弃不离 2025-02-10 00:42:38

使用createHtmloutputfromfile('filename')而不是

基于您的设置,我发现您只需要使用createHtmloutputfromfile()将HTML脚本链接到Google Apps脚本。对于测试用例,我使用了以下脚本:

function onOpen(e)
{
    DocumentApp.getUi().createAddonMenu().addItem('Open dialog', 'openDialog').addItem('Open html', 'openHtml').addToUi();
}
function openDialog() {
    DocumentApp.getUi().alert('This works');
}
function openHtml() {
    var output = HtmlService.createHtmlOutputFromFile('test.html'); //use createHtmlOutputFromFile instead
    output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
    DocumentApp.getUi().showModalDialog(output, 'Title');
}

另一方面,我使用了您在示例中使用的HTML。

要将您的HTML添加到Google Apps脚本项目中,只需单击“文件”以外的加号。在此测试案例中,我以文件名“ test.html”保存了您的HTML。

输出

”

参考

Use createHtmlOutputFromFile('filename') Instead

Based on your setup, I found that you just need to link your html script to your google apps script using createHtmlOutputFromFile(). For my test case, I used the following script:

function onOpen(e)
{
    DocumentApp.getUi().createAddonMenu().addItem('Open dialog', 'openDialog').addItem('Open html', 'openHtml').addToUi();
}
function openDialog() {
    DocumentApp.getUi().alert('This works');
}
function openHtml() {
    var output = HtmlService.createHtmlOutputFromFile('test.html'); //use createHtmlOutputFromFile instead
    output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
    DocumentApp.getUi().showModalDialog(output, 'Title');
}

On the other hand, I used the same html that you used in your example.

To add your html to the google apps script project, just click on the plus symbol besides "Files". In this test case, I saved your html under the file name "test.html".

Files List

Output

iframe

Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文