Google Apps script not working in an embedded iframe

只谈情不闲聊 提交于 2021-02-08 11:42:50

问题


I have a Google sheet where I have placed a few buttons linking to the script. It works fine when I use it on the sheets page. But when I publish it and embed the sheet on my website, the button doesn't work. It's not performing. Below is the sample code I use to move from one sheet to another with the click of the button and the iframe code I am using.

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>

回答1:


When you publish a a Google spreadsheet to the Web - it is view only access

  • Indeed, if you type into your browser address bar the direct publish link: https://docs.google.com/spreadsheets/d/e/xxxxxxxxxxxxxxxxx/pubhtml?widget=true&amp;headers=false, you will see that you likewise obtain only a view version of the spreadsheet
  • You cannot perform edits in the spreadsheets, and consequently you cannot run an Apps Script - since the latter requires edit access

WORKAROUND

  • When publishing a spreadsheet, you can specify in the URL which sheet shall be shown
  • By default, it is the first sheet, but if you specofy e.g. https://docs.google.com/spreadsheets/d/e/xxxxxxxxxxxxxxxxx/pubhtml?widget=true&amp;headers=false#gid=yyyyyyyyy, then the sheet with the sheet id yyyyyyyyy will be opened
  • Consequently, you can create your own sheet changing script within your Website
  • For this, simply create an html button and specify that on button click the user shall be redirected to the sheet

Sample

    <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>


来源:https://stackoverflow.com/questions/64448580/google-apps-script-not-working-in-an-embedded-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!