UI到HTML,转换不会将日期写入表单部分2 [英] UI to HTML, conversion will not write a date to the sheet part 2

查看:76
本文介绍了UI到HTML,转换不会将日期写入表单部分2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续我在这里发布的原始问题:
UI到HTML第1部分,这是由@SandyGood回应(谢谢桑迪)我现在已经让自己陷入了另一个困境。



MyForm.HTML代码是:

 <! - 使用模板化HTML打印scriptlet导入常用样式表。 - > 
<?!= HtmlService.createHtmlOutputFromFile('Stylesheet')。getContent(); ?>
< html>
< body>
< div>
<! - 页眉标题& '完成警告 - >
< span class =grey>< b> ProReactive Log Form v3.0.74< / b>< / span>
< h4>完成所有栏位以确保表格已成功处理< / h4>

< form role =formaction =mail.phpmethod =post>

< input type =datename =dateAid =dateAclass =form-controlplaceholder =Enter Date>

< input type =textname =sitelist =depotslistid =siteclass =form-controlplaceholder =Select depot / site>
< datalist id =depotslist>
< option value =one>
< option value =two>
< / datalist>

< input type =textname =reporterid =reporterclass =form-controlplaceholder =Enter Namestyle =width:150px>

< input type =textname =phoneid =phoneclass =form-controlplaceholder =Phone numberstyle =width:150px>

< input type =textname =sourcelist =sourcelistid =sourceclass =form-controlplaceholder =Select Source Code>
< datalist id =sourcelist>
< option value =one>
< option value =two>
< / datalist>

< datalist id =hazardlist>
< option value =one>
< option value =two>
< / datalist>

< input type =textname =briefid =briefclass =form-controlplaceholder =Enter BRIEF Details>

< input type =textname =describeid =describeclass =form-controlplaceholder =FULL Description& Location>

< datalist id =prioritylist>
< option value =one>
< option value =two>
< / datalist>

< button type =shareid =submit-btnonclick =runGoogleScript()class =btn btn-default>提交< / button>
< / form>

< / div>
< / body>
< / html>

< script>
函数onSuccess(argReturnValue){
alert('was successful'+ argReturnValue);
//屏幕上的ResetFields
Document.getElementById(dateA)。value =;
}

函数runGoogleScript(){
console.log('v3.0.74 ran!');
var d = document.getElementById(dateA)。value;
var inputDa = d.toString();
var inputDe = document.getElementById(site).value;
google.script.run.withSuccessHandler(onSuccess)
.InsertInSS(inputDa);
};
< / script>

和我的.gs代码是:

 函数InsertInSS(argPassedInName){
var ssKey ='1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
var SS = SpreadsheetApp.openById(ssKey);
var Sheet = SS.getSheetByName('LOG');
Sheet.getRange(Sheet.getLastRow()+ 1,1,1).setValue(argPassedInName);
}

但我错过了一些内容,因为我只能管理将第一个字段电子表格。我曾尝试添加行中字段的名称

  .InsertInSS(inputDa); 

例如
.InsertInSS(inputDa,inputDe);
,我试着复制不同的输入行,例如

  .InsertInSS(inpuDa ); 
.InserInSS(inputDe);

我尝试了绝望的行更改,但没有多大意义,所以我将它们还原为他们也没有工作。我还没有做什么?

另外,我想开始发布第2列中的所有数据,因为第1列需要捕获userID。

  var thisUser = Session.getActiveUser()。getEmail()); 

但是应该在哪里插入?



编辑7月16日



我已经插入了建议的代码,但却收到错误:

  function runGoogleScript(){
console.log('v3.0.74 ran!');
var d = document.getElementById(dateA)。value;
var inputDa = d.toString();
var inputDe = document.getElementById(site).value;
var arrayToPass = [];
arrayToPass.push(inputDa);
arrayToPass.push(inputDe);
google.script.run.withSuccessHandler(onSuccess)
.InsertInSS(arrayToPass);
};

函数InsertInSS(myDataInAnArray){

var i = 0,thisValue =;
for(i = 0; i< myDataInAnArray.length; i + = 1){
thisValue = myDataInAnArray [i];

var ssKey ='1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
var SS = SpreadsheetApp.openById(ssKey);
var Sheet = SS.getSheetByName('LOG');

Sheet.getRange(Sheet.getLastRow()+ 1,1,1,1).setValue(thisValue);





产生错误:如果我运行它因为它是我得到的


TypeError:无法从undefined读取属性length。 (第4行,文件)


如果我在 var myDataInAnArray = [] 我得到代码运行,但电子表格中没有任何反应。数组内容的日志是空白的。



编辑7月21日
我现在感觉很接近失败。烦人。我一直在围绕着,我知道这是我完全缺乏理解力而徘徊于我。我已经将.HTML中的代码改为:

arrayToPass.push(inputDa);
arrayToPass.push(inputDe);


var myDataInAnArray =(arrayToPass)
google.script.run.withSuccessHandler(onSuccess)
Console.log(myDataInAnArray);
.InsertInSS(myDataInAnArray);

以及.gs:

  var i = 0,thisValue =; 
var myDataInAnArray = [arrayToPass]
for(i = 0; i< myDataInAnArray.length; i + = 1){
thisValue = myDataInAnArray [i];

var ssKey ='1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
var SS = SpreadsheetApp.openById(ssKey);
var Sheet = SS.getSheetByName('LOG');

Sheet.getRange(Sheet.getLastRow()+ 1,1,1).setValue(thisValue);



$ / code $ / pre

所有我能得到的是新行在第一个单元格中输入未定义的电子表格。我觉得我已经改变了我所能做的一切,但我非常绿色和不熟练,我承认我并不真正知道自己在做什么。为什么数据不会从网站表单到电子表格?

解决方案

好吧,这是一个难题,但我有

 > function runGoogleScript(){
console.log('v3.0.151 ran!');

//第一个标题条目
var datA = document.getElementById(dateA).value;
var inputDa = datA.toString();
var inputDe = document.getElementById(site).value;
var inputRe = document.getElementById(reporter)。value;
var inputPh = document.getElementById(phone)。value;

//第二个标题条目
var inputSo = document.getElementById(source).value;
var inputHa = document.getElementById(hazard)。value;
var inputBr = document.getElementById(brief).value;
var inputDs = document.getElementById(describe)。value;
var inputPr = document.getElementById(priority)。value;

google.script.run.withSuccessHandler(onSuccess)

var arrayToPass = [inputDa,inputDe,inputRe,inputPh,inputSo,inputHa,inputBr,inputDs,inputPr];


var myDataInAnArray =(arrayToPass)
google.script.run.withSuccessHandler(onSuccess)
.InsertInSS(myDataInAnArray);


console.log(arrayToPass);

};

在我的.gs表中,我有这个:

 函数InsertInSS(arrayToPass){

var i = 0,thisValue =;
var myDataInAnArray = [arrayToPass]
for(i = 0; i< myDataInAnArray.length; i + = 1){
thisValue = myDataInAnArray [i];

var ssKey ='1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
var SS = SpreadsheetApp.openById(ssKey);
var Sheet = SS.getSheetByName('LOG');

Sheet.getRange(Sheet.getLastRow()+ 1,1,1,9).setValues(myDataInAnArray);

Logger.log(myDataInAnArray);

}
}



它的工作原理。我最终坚持使用的是 Sheet.getRange(Sheet.getLastRow()+ 1,1,1,9).setValues(myDataInAnArray); 因为我没有声明我创建的数组,所以它无法在任何地方写入。



感谢Sandy的帮助和感谢Google Apps Script [第2版](O'Reilly Media)的解释。

Further to my original question which I posted here: UI to HTML part 1 which was answered by @SandyGood (thanks Sandy) I have now posed myself another quandary.

MyForm.HTML code is:

<!-- Use a templated HTML printing scriptlet to import common stylesheet. -->
<?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>
<html>
  <body>
  <div>
  <!-- Page header title & 'completion warning -->
  <span class="grey"><b>ProReactive Log Form v3.0.74</b></span>
  <h4>Complete ALL fields to ensure the form is processed successfully</h4>

  <form role="form" action="mail.php" method="post">

                <input type="date" name="dateA" id="dateA" class="form-control" placeholder="Enter Date">

                <input type="text" name="site" list="depotslist" id="site" class="form-control" placeholder="Select depot/site">
                <datalist id="depotslist">
                <option value="one">
                <option value="two">
                </datalist>

                <input type="text" name="reporter" id="reporter" class="form-control" placeholder="Enter Name" style="width: 150px">

                <input type="text" name="phone" id="phone" class="form-control" placeholder="Phone number" style="width: 150px">

                <input type="text" name="source" list="sourcelist" id="source" class="form-control" placeholder="Select Source Code">
                <datalist id="sourcelist">
                <option value="one">
                <option value="two">
                </datalist>

                <input type="text" name="hazard" list="hazardlist" id="hazard" class="form-control" placeholder="Select Hazard Code">
                <datalist id="hazardlist">
                <option value="one">
                <option value="two">
                </datalist>

                <input type="text" name="brief" id="brief" class="form-control" placeholder="Enter BRIEF Details">

                <input type="text" name="describe" id="describe" class="form-control" placeholder="FULL Description & Location">

                <input type="text" name="priority" list="prioritylist" id="priority" class="form-control" placeholder="Select Priority" style="width: 150px">
                <datalist id="prioritylist">
                <option value="one">
                <option value="two">
                </datalist>

                <button type="share" id="submit-btn" onclick="runGoogleScript()" class="btn btn-default">Submit</button>
</form>

  </div>
  </body>
  </html>

  <script>
  function onSuccess(argReturnValue){
      alert('was successful ' +argReturnValue);
      //ResetFields on Screen
      Document.getElementById("dateA").value = "";
      }

  function runGoogleScript() {
      console.log('v3.0.74 ran!');
      var d = document.getElementById("dateA").value;
      var inputDa = d.toString();
      var inputDe = document.getElementById("site").value;
      google.script.run.withSuccessHandler(onSuccess)
      .InsertInSS(inputDa);
      };
  </script>

and my .gs code is:

function InsertInSS(argPassedInName) {
  var ssKey = '1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
  var SS = SpreadsheetApp.openById(ssKey);
  var Sheet = SS.getSheetByName('LOG');
  Sheet.getRange(Sheet.getLastRow()+1,1,1).setValue(argPassedInName);
  }

but I am missing something as I can only manage to post the first field to the spreadsheet. I have tried adding in the names of the fields in the line

.InsertInSS(inputDa);

For example .InsertInSS(inputDa, inputDe); and I have tried replicating the line with the different inputs e.g.

.InsertInSS(inpuDa);
.InserInSS(inputDe);

and I have tried desperate line changes that didn't make a lot of sense so I reverted them as they didn't work either. What am I not doing?

Also, I want to start posting all the data in column 2 as column 1 needs to capture the userID.

var thisUser = Session.getActiveUser().getEmail());

But where should that be inserted?

EDIT 16th July

I have inserted the suggested code, but get errors:

 function runGoogleScript() {
      console.log('v3.0.74 ran!');
      var d = document.getElementById("dateA").value;
      var inputDa = d.toString();
      var inputDe = document.getElementById("site").value;
      var arrayToPass = [];
      arrayToPass.push(inputDa);
      arrayToPass.push(inputDe);
      google.script.run.withSuccessHandler(onSuccess)
      .InsertInSS(arrayToPass);
      };

function InsertInSS(myDataInAnArray) {

var i=0, thisValue="";
for (i=0;i<myDataInAnArray.length;i+=1) {
thisValue = myDataInAnArray[i];

  var ssKey = '1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
  var SS = SpreadsheetApp.openById(ssKey);
  var Sheet = SS.getSheetByName('LOG');

Sheet.getRange(Sheet.getLastRow()+1,1,1,1).setValue(thisValue);

}
}

Errors produced: if I run it as it is I get

TypeError: Cannot read property "length" from undefined. (line 4, file "")

If I add in the line var myDataInAnArray = [] I get the code to run but nothing happens in the spreadsheet. A log of the array contents is blank.

Edit 21st July I'm feeling close to defeat now. Annoyingly. I have been going round in circles and I know it is my complete lack of comprehension that is hobbling me. I have changed my code in the .HTML now to:

var arrayToPass = [];
  arrayToPass.push(inputDa);
  arrayToPass.push(inputDe);


  var myDataInAnArray = (arrayToPass)
  google.script.run.withSuccessHandler(onSuccess)
  Console.log(myDataInAnArray);
  .InsertInSS(myDataInAnArray);

and in the .gs to:

var i=0, thisValue="";
var myDataInAnArray = [arrayToPass]
for (i=0;i<myDataInAnArray.length;i+=1) {
thisValue = myDataInAnArray[i];

  var ssKey = '1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
  var SS = SpreadsheetApp.openById(ssKey);
  var Sheet = SS.getSheetByName('LOG');

Sheet.getRange(Sheet.getLastRow()+1,1,1).setValue(thisValue);

}
}

and all I can get is a new row entered into the spreadsheet with "undefined" in the first cell. I feel I have changed everything I can but I am so green and unskilled I admit I don't really know what I am doing. Why is the data not making it from the website form to the spreadsheet?

解决方案

Well, it's been a slog, but I have finally done it!

In my form I have this code:

      function runGoogleScript() {
      console.log('v3.0.151 ran!');

      //First header entries
      var datA = document.getElementById("dateA").value;
      var inputDa = datA.toString();            
      var inputDe = document.getElementById("site").value;
      var inputRe = document.getElementById("reporter").value;
      var inputPh = document.getElementById("phone").value;

      //Second header entries
      var inputSo = document.getElementById("source").value;
      var inputHa = document.getElementById("hazard").value;
      var inputBr = document.getElementById("brief").value;
      var inputDs = document.getElementById("describe").value;
      var inputPr = document.getElementById("priority").value;

      google.script.run.withSuccessHandler(onSuccess)

      var arrayToPass = [inputDa, inputDe, inputRe, inputPh, inputSo, inputHa, inputBr, inputDs, inputPr];


      var myDataInAnArray = (arrayToPass)
      google.script.run.withSuccessHandler(onSuccess)
      .InsertInSS(myDataInAnArray);


console.log(arrayToPass);

   };

And in my .gs sheet I have this:

function InsertInSS(arrayToPass) {

var i=0, thisValue="";
var myDataInAnArray = [arrayToPass]
for (i=0;i<myDataInAnArray.length;i+=1) {
thisValue = myDataInAnArray[i];

  var ssKey = '1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
  var SS = SpreadsheetApp.openById(ssKey);
  var Sheet = SS.getSheetByName('LOG');

Sheet.getRange(Sheet.getLastRow()+1,1,1,9).setValues(myDataInAnArray);

Logger.log(myDataInAnArray);

} }

And it works. The line I was stuck with in the end turned out to be Sheet.getRange(Sheet.getLastRow()+1,1,1,9).setValues(myDataInAnArray); as I wasn't declaring the array I was creating so it couldn't write it anywhere.

Thanks to Sandy for the assistance and thanks to Google Apps Script [2nd Edition] (O'Reilly Media) for the explanation.

这篇关于UI到HTML,转换不会将日期写入表单部分2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆