包含参数时对函数的调用不起作用

时间:2018-07-20 18:17:42

标签: javascript html google-apps-script

我已经尝试了好几个星期,反复无常地解决这个问题。也许其他人会看到我想念的明显东西。

在这一点上,我只是在尝试验证该功能是否正常运行。除了检查参数从HTML按钮到函数的传递外,我没有做任何其他事情。

此处有两个正在播放的按钮。第一个发送参数,但不起作用-不调用Logger.log(“ yeeHa”)语句。秒按钮不发送任何参数,但确实允许调用Logger语句。

function runSetup(projectTitle,projectMgr,projectSponsor,datepicker,weeks) {
//function runSetup() {
    Logger.log("before the vars");
    var ptitle = projectTitle;
    var pMan = projectMgr;
    var pSpon = projectSponsor;
    var date = datepicker;
    var wks = weeks;
    var tHeader = "Project";
    var mHeader = "Project Manager";
    var sHeader = "Project Sponsor";
    Logger.log("yeeHa");
}
<!DOCTYPE html>
<html>
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<head>
      
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <script>
         $( function() {
           $( "#datepicker" ).datepicker();
       } );
    </script>
    <base target="_top">
  </head>
  <body>
  <div id="container" style="padding: 10px; text-align:center;">
  
  <strong>Project Information</strong>
  <p></p>
	
  <div class="inline form-group">
    <label for="projectTitle">Project Title</label>
    <input type="text" id="projectTitle" style="width: 150px;">
  </div>
  <p></p>
  <div class="inline form-group">
    <label for="projectMgr">Project Manager</label>
    <input type="text" id="projectMgr" style="width: 150px;"> 
    <br>
  </div>
  
  <div class="inline form-group">
    <label for="projectSponsor">Project Sponsor</label><br>
    <input type="text" id="projectSponsor" style="width: 150px;"> 
    <p></p>

  </div>
  
  <input type="text" id="datepicker" value="start date">
  
<div class="inline form-group">
  <label for="state">Weeks</label>
  <input type="text" id="state" style="width: 40px;">
</div>
  
  <p></p>
  <!-- THIS BUTTON DOES NOT WORK -->
  <input type="button" class="create" name="ganntSetup" value="Setup Gannt" onClick="google.script.run.runSetup
    (document.getElementById('projectTitle').value;
     document.getElementById('projectMgr').value;
     document.getElementById('projectSponsor').value;
     document.getElementById('datepicker').value;
     document.getElementById('weeks').value;
  )" />
  <!-- THIS BUTTON WORKS BUT DOES NOT SEND NEEDED INFORMATION -->
  <input type="button" class="create" name="ganntSetup2" value="Setup Gannt2" onClick="google.script.run.runSetup()"/>
  
  <div>
  <p></p>
  <p></p>
  <input type="button" name="Main Menu" value="Main Menu" onClick="google.script.run.mainSidebar()" />
  </div>
  
  </div>
    
  <div style=
        "width: 100%;
        position: fixed;
        bottom: 0;
        width: 100%; 
        text-align:center;
        background-color:black;
        text-color:white;">
        <p><a href="http://www.fastadmin.solutions" target="blank"><b>©FastAdmin Solutions</b></a></p>
        </div>  
        </div>
  </body>
</html>

不确定我所缺少的是不允许它处理的事情。

2 个答案:

答案 0 :(得分:1)

函数调用中的每个项目后面都有分号,我不是JS专业人士,但是分号是行终止符,所以它们不应该是逗号吗?

onClick="google.script.run.runSetup
    (document.getElementById('projectTitle').value,
     document.getElementById('projectMgr').value,
     document.getElementById('projectSponsor').value,
     document.getElementById('datepicker').value,
     document.getElementById('weeks').value
  )"

除此之外,这些函数接收的值到底是什么?也许尝试比较收到的内容与发送的内容。

答案 1 :(得分:1)

应使用逗号(,)而不是分号(;)分隔要设置为函数参数的变量的值。另外,具有weeks值的输入具有状态ID,但是您尝试通过使用document.getElementById(“ weeks”)来获取其值。

<!DOCTYPE html>
<html>
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<head>
      
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <script>
         $( function() {
           $( "#datepicker" ).datepicker();
       } );
    </script>
    <base target="_top">
  </head>
  <body>
  <div id="container" style="padding: 10px; text-align:center;">
  
  <strong>Project Information</strong>
  <p></p>
	
  <div class="inline form-group">
    <label for="projectTitle">Project Title</label>
    <input type="text" id="projectTitle" style="width: 150px;">
  </div>
  <p></p>
  <div class="inline form-group">
    <label for="projectMgr">Project Manager</label>
    <input type="text" id="projectMgr" style="width: 150px;"> 
    <br>
  </div>
  
  <div class="inline form-group">
    <label for="projectSponsor">Project Sponsor</label><br>
    <input type="text" id="projectSponsor" style="width: 150px;"> 
    <p></p>

  </div>
  
  <input type="text" id="datepicker" value="start date">
  
<div class="inline form-group">
  <label for="weeks">Weeks</label>
  <input type="text" id="weeks" style="width: 40px;">
  <!--Changed id to weeks instead of state.-->
</div>
  
  <p></p>
  <!–– THIS BUTTON DOES NOT WORK ––>
  <input type="button" class="create" name="ganntSetup" value="Setup Gannt" onClick="runSetup
    (document.getElementById('projectTitle').value,     document.getElementById('projectMgr').value,   document.getElementById('projectSponsor').value,
     document.getElementById('datepicker').value,
     document.getElementById('weeks').value
  )" />
  <!–– THIS BUTTON WORKS BUT DOES NOT SEND NEEDED INFORMATION ––>
  <input type="button" class="create" name="ganntSetup2" value="Setup Gannt2" onClick="google.script.run.runSetup()"/>
  
  <div>
  <p></p>
  <p></p>
  <input type="button" name="Main Menu" value="Main Menu" onClick="google.script.run.mainSidebar()" />
  </div>
  
  </div>
    
  <div style=
        "width: 100%;
        position: fixed;
        bottom: 0;
        width: 100%; 
        text-align:center;
        background-color:black;
        text-color:white;">
        <p><a href="http://www.fastadmin.solutions" target="blank"><b>©FastAdmin Solutions</b></a></p>
        </div>  
        </div>
        <script>
        
        function runSetup(projectTitle,projectMgr,projectSponsor,datepicker,weeks) {
//function runSetup() {
    console.log("before the vars");
    //Logger.log("before the vars");
    var ptitle = projectTitle;
    var pMan = projectMgr;
    var pSpon = projectSponsor;
    var date = datepicker;
    var wks = weeks;
    var tHeader = "Project";
    var mHeader = "Project Manager";
    var sHeader = "Project Sponsor";
    //Logger.log("yeeHa");
    console.log("yeeHa");
}
</script>
  </body>
</html>