2017-10-19 84 views
0

我正在將谷歌表單作爲iFrame嵌入Google電子表格中的模式對話框(它已經可以通過客戶菜單訪問)並且動態更改iFrame代碼基於電子表格中正在發生的事情。谷歌腳本從谷歌表單元格中提取動態HTML iFrame

我的谷歌腳本引用HTML文件是:

//Help Guide 
    function openHelp() { 
    var html = HtmlService.createHtmlOutputFromFile('Help Guide').setHeight(560).setWidth(814.81).setSandboxMode(HtmlService.SandboxMode.IFRAME); 
    SpreadsheetApp.getUi().showModalDialog(html, ' '); 
} 

當我產生的iFrame代碼粘貼到HTML文件「幫助指南」,它加載任意的。但是,當我從菜單運行openHelp()函數時,我想引用放置在Sheet1!C1上的我的iFrame代碼。這將使我能夠根據電子表格動態更改iFrame。我已經探索過使用谷歌的HTML模板服務,但一直未能弄清楚。

我確定該腳本非常簡單,但我不熟悉需要在HTML文件中編寫哪些代碼以從Sheet1!C1中提取生成的iframe代碼。

謝謝!

回答

0

獲取電子表格

  1. 您需要更新變量iframeSheet和iframeCell從細胞一些HTML代碼。
  2. 創建iframe語句。
  3. 部署爲網絡應用程序。 doGet已經在那裏了。你也可以運行showIframeDialog()函數來查看它的工作。

驗證碼:

function getIframeCode() 
{ 

    var iframeSheet='Sheet1'; 
    var iframeCell='A1'; 
    var ss=SpreadsheetApp.getActive(); 
    var sh=ss.getSheetByName(iframeSheet); 
    var hl=sh.getRange(iframeCell).getValue(); 
    return hl; 
} 

function showIframeDialog() 
{ 
    var ui=HtmlService.createHtmlOutputFromFile('iframecode'); 
    SpreadsheetApp.getUi().showModelessDialog(ui, 'Dynamic Iframe') 
} 

function doGet() 
{ 
    var ui=HtmlService.createHtmlOutputFromFile('iframecode'); 
    return ui.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL); 
} 

的iframecode.html文件:

<!DOCTYPE html> 
<html> 
    <head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <script> 
    $(function() { 
     google.script.run 
      .withSuccessHandler(updateIframeCode) 
      .getIframeCode(); 
     }); 
    function updateIframeCode(hl) 
    { 
     document.getElementById("iframe").innerHTML=hl; 
    }  
    console.log("My code"); 
    </script> 
    </head> 
    <body> 
    <div id="iframe"></div> 
</body> 
</html> 
+0

謝謝您的幫助。一切都很完美! –