【问题标题】:html sidebar data in not accessiblehtml侧边栏数据无法访问
【发布时间】:2020-12-31 21:18:58
【问题描述】:

我有一个谷歌表,我打开了脚本编辑器并打开了两个文件,一个脚本文件和一个 html 文件。这是我的脚本文件中的内容:

//to load html sidebar
function loadHome() {
  const htmlSidebar = HtmlService.createTemplateFromFile("home");
  const htmlOutput = htmlSidebar.evaluate();
  const ui = SpreadsheetApp.getUi();
  ui.showSidebar(htmlOutput);
}

//to create a custom menu
function createMenu(){
   const ui = SpreadsheetApp.getUi();
   const menu = ui.createMenu("AC Paz");
   menu.addItem("Side Registration", "loadHome");
    menu.addItem("Order Framing", "loadFraming");
   menu.addToUi();
}

//to make sure custom menu is loaded on open
function onOpen(){
  createMenu();
}

//to access data passed on html input fields
function userClicked(userInfo){
 
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var ws = ss.getSheetByName("Site Information");

  ws.appendRow([userInfo.firstName, userInfo.lastName, new Date()])
}

这是我的 html 文件中的内容:

    <!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    
    <div >
            <input id="fn" type="text" >
            <label for="fn">First Name</label>
    </div>
    <div >
            <input id="ln" type="text">
            <label for="ln">Last Name</label>
    </div>
    
    <button id="btn">Add</button>
       <script>
        document.getElementById("btn").addEventListener("click",doStuff);
        
        function doStuff(){
        
        var userInfo = {}
        
        userInfo.firstName = document.getElementById("fn").value;
        userInfo.lastName = document.getElementById("ln").value;
        google.script.run.userClicked(userInfo);
        document.getElementById("fn").value = "";
        document.getElementById("ln").value = "";
          }
        </script>

这是我的问题:当我在输入字段中输入内容并单击按钮时,没有任何内容传递到电子表格中,但是输入字段中的条目会被清除。

【问题讨论】:

  • 我试过你的代码。它按预期工作。唯一没有传递到电子表格的情况是缺少“站点信息”表时。请检查您的电子表格中是否存在“站点信息”表。如果这不能解决您的问题,请分享示例文档
  • 感谢您的回复!我将脚本复制粘贴到另一个帐户下的新文件中,它可以工作。奇怪的。但是,该工作表确实存在于原始电子表格中。原始的 Google 表格位于企业帐户中,这会导致问题吗?
  • 我真的不知道,如果您可以使用正常程序(没有侧边栏输入)将值写入单元格,那么使用的帐户应该没有任何问题。如果您无法在电子表格中写入/读取值,则帐户可能存在问题。当您最初运行/使用应用程序脚本功能时,它将打开一个授权请求以查看、编辑、创建和删除您的电子表格

标签: javascript html google-apps-script sidebar


【解决方案1】:

试试这个:

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    
    <div >
            <input id="fn" type="text" >
            <label for="fn">First Name</label>
    </div>
    <div >
            <input id="ln" type="text">
            <label for="ln">Last Name</label>
    </div>
    
    <button id="btn">Add</button>
       <script>
       window.onload=function(){
        document.getElementById("btn").addEventListener("click",doStuff);
        
        function doStuff(){
        
        var userInfo = {}
        
        userInfo.firstName = document.getElementById("fn").value;
        userInfo.lastName = document.getElementById("ln").value;
        google.script.run
        .withSuccessHandler(function(){
          document.getElementById("fn").value = "";
          document.getElementById("ln").value = "";   
        })
        .userClicked(userInfo);
        
          }
          }
        </script>
        </body>
        </html>
    

GS:

function userClicked(userInfo){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ws = ss.getSheetByName("Sheet1");
  ws.appendRow([userInfo.firstName, userInfo.lastName, new Date()])
}

function showMySideBar() {
  SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('ah1'))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-29
    • 1970-01-01
    • 2018-08-16
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多