【问题标题】:Google Apps script not working in an embedded iframeGoogle Apps 脚本在嵌入式 iframe 中不起作用
【发布时间】:2020-10-20 15:38:42
【问题描述】:

我有一个 Google 表格,其中放置了几个链接到脚本的按钮。当我在工作表页面上使用它时效果很好。但是当我发布它并将工作表嵌入我的网站时,该按钮不起作用。它没有表演。 下面是我用来通过单击按钮和我正在使用的 iframe 代码从一张纸移动到另一张纸的示例代码。

function goToInward() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName("Inward"));
}

<iframe src="https://docs.google.com/spreadsheets/d/e/xxxxxxxxxxxxxxxxx/pubhtml?widget=true&amp;headers=false" frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:55px;left:0px;right:0px;bottom:0px" height="100%" width="100%"></iframe>

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    当您将 Google 电子表格发布到网络时 - 只能查看访问权限

    • 确实,如果您在浏览器地址栏中输入直接发布链接: https://docs.google.com/spreadsheets/d/e/xxxxxxxxxxxxxxxxx/pubhtml?widget=true&amp;amp;headers=false, 您会看到您同样只获得了电子表格的视图版本
    • 您无法在电子表格中执行编辑,因此您无法运行 Apps 脚本 - 因为后者需要编辑权限

    解决方法

    • 发布电子表格时,您可以在 URL 中指定应显示的工作表
    • 默认情况下,它是第一张纸,但如果您指定例如 https://docs.google.com/spreadsheets/d/e/xxxxxxxxxxxxxxxxx/pubhtml?widget=true&amp;amp;headers=false#gid=yyyyyyyyy, 然后将打开工作表 ID 为 yyyyyyyyy 的工作表
    • 因此,您可以在自己的网站中创建自己的换页脚本
    • 为此,只需创建一个 html 按钮并指定在单击按钮时将用户重定向到工作表

    示例

        <iframe id="frame" src="https://docs.google.com/spreadsheets/d/e/xxxxxxxxxxxxxxxxx/pubhtml?widget=true&amp;headers=false" frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:55px;left:0px;right:0px;bottom:0px" height="100%" width="100%"></iframe>
        <button onclick="myFunction()">Change tabs</button>
        <script>
          function myFunction(){
            document.getElementById("frame").src = "https://docs.google.com/spreadsheets/d/e/xxxxxxxxxxxxxxxxx/pubhtml?widget=true&amp;headers=false#gid=yyyyyyyyy";
          }
        </script>
    

    【讨论】:

    • 我尝试了你的建议。但该工作表不可编辑。它看起来像一张普通的图片,没有点击和编辑的选项
    • 如果您使用发布到网络的版本,则该工作表永远不可编辑。如果要对其进行编辑,则需要将实际工作表 URL(而不是发布 URL)嵌入到您的网站中,并相应地设置共享权限。但这已经是另一回事了。
    猜你喜欢
    • 2017-04-12
    • 2021-05-29
    • 2020-11-19
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多