Google Apps代码会运行,但会显示"不支持返回类型"

时间:2016-07-21 09:20:05

标签: google-apps-script

我正在从头开始构建自己的Html表单,该表单与某个电子表格相关联。 我成功地将表单数据发送到电子表格中,但是当我按下表单发送按钮时,另一个选项卡显示,我收到此错误消息:

  

脚本已完成,但返回的值不是受支持的返回类型。

虽然我的程序按我的意愿运行,但每次都会收到此错误消息。

我使用doGet和doPost将数据发送到电子表格。

这是我的doGet代码:

function doGet(){
var html = HtmlService
.createHtmlOutputFromFile('body')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
return html;
}

这是我的doPost代码:

function doPost(e) { 
 var ss = SpreadsheetApp.openById("id");
 var Properties = PropertiesService.getScriptProperties();
 var sheet = ss.getSheetByName("sheet1");
 var uid = Properties.getProperty("uid");
 var uid = Number(uid) + 1;
 var date = Utilities.formatDate(new Date(), "JST", "yyyy/MM/dd HH:mm:ss");

 var array = [uid,date,e.parameter.pref,e.parameter.area,e.parameter.building];
 sheet.appendRow(array);
 Properties.setProperty("uid",uid);
}

这是附加信息(HTML和Javascripts)

HTML code:

<body>
  <iframe src="#" id="frame" name="frame" style="width:0px; height:0px; border:0px;">
  </iframe>
  <div class="center" id="main">
      <h3>Form Title</h3>
      <form id="form" action="web app URL" method="post" target="frame" name="form">
      <div class="left">
        <p class="custom" align="left">
            Prefecture:<br>
            <select name="pref" onChange="functionName1()" required>
                <option selected>Choose...</option>
                <option>Tokyo</option>
                <option>Yamanashi</option>
            </select><span class="required">*</span>
        </p>
        <p class="custom" align="left">
            Area:<br>
            <select name="area" onChange="functionName2()" required>
                <option>Choose...</option>
            </select><span class="required">*</span>
        </p>
        <p class="custom" align="left">
            Building:<br>
            <select name="building" required>
                <option>Choose...</option>
            </select><span class="required">*</span>
        </p>
        <p class="center">
            <input type="button" value="送信" class="btn" onClick="formsend()">
        </p>
      </div>
    </form>
</div>
<script type="text/javascript">

function formsend(){
   if(window.confirm("Are you sure to submit?")){
   document.getElementById("form").submit();
   document.getElementById("main").innerHTML = "<p><b><div style='color:red; font-size:16pt;'>Form sent.</div></b></p><input type='button' value='戻る' onClick='reload()'>";
   }
   else{window.alert("Cancelled");
   }
}

function reload(){
    location.href = "Web App URL";
}
</script>

1 个答案:

答案 0 :(得分:1)

正如Sandy Good建议的那样,&#39;目标&#39;属性。与服务器通信的最佳方式是google.script.run API我对您的代码进行了一些更改作为示例:

Google Apps脚本:

function doGet(){
  var html = HtmlService
  .createHtmlOutputFromFile('body')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);
 return html;
}

function doSomething(e) { 
 var ss = SpreadsheetApp.openById("id");
 var Properties = PropertiesService.getScriptProperties();
 var sheet = ss.getSheetByName("sheet1");
 var uid = Properties.getProperty("uid");
 var uid = Number(uid) + 1;
 var date = Utilities.formatDate(new Date(), "JST", "yyyy/MM/dd HH:mm:ss");

 var array = [uid,date,e.pref,e.area,e.building];
 sheet.appendRow(array);
 Properties.setProperty("uid",uid);
}

HTML code:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  <div class="center" id="main">
      <h3>Form Title</h3>
      <form id="form" action="web app URL" name="form">
      <div class="left">
        <p class="custom" align="left">
            Prefecture:<br>
            <select name="pref" onChange="functionName1()" required>
                <option selected>Choose...</option>
                <option>Tokyo</option>
                <option>Yamanashi</option>
            </select><span class="required">*</span>
        </p>
        <p class="custom" align="left">
            Area:<br>
            <select name="area" onChange="functionName2()" required>
                <option>Choose...</option>
            </select><span class="required">*</span>
        </p>
        <p class="custom" align="left">
            Building:<br>
            <select name="building" required>
                <option>Choose...</option>
            </select><span class="required">*</span>
        </p>
        <p class="center">
            <input type="button" value="送信" class="btn" onClick="formsend()">
        </p>
      </div>
    </form>
</div>
  </body>
</html>
<script type="text/javascript">

function formsend(){
   if(window.confirm("Are you sure to submit?")){
   google.script.run.doSomething(document.forms[0]);
   document.getElementById("main").innerHTML = "<p><b><div style='color:red; font-size:16pt;'>Form sent.</div></b></p><input type='button' value='戻る' onClick='reload()'>";
   }
   else{window.alert("Cancelled");
   }
}
</script>