将数据插入工作表/工作表并发送电子邮件

时间:2017-09-19 05:14:27

标签: javascript html google-apps-script google-sheets

我创建了脚本,将信息添加到多个工作表中。但是,当我只想在一张纸上添加信息时,它不起作用。我应该在这个剧本中改变什么?这是我上一个主题的链接:enter link description here

 <b>test 1.1</b><br />
   
<br>
    <form>
   <br>
  Nr item:<br>
    <input id="NrItem" name="NrItem" type="text" />
 <br>

<br>
<input  name="choices" type="checkbox" value="Sheet1" /> sheet1
<br>
<br>
<input  name="choices" type="checkbox" value="Sheet2" /> sheet2
<br>
<br>
<input name="choices" type="checkbox" value="Sheet3" /> sheet3
<br>

  <br>
<br>
   <input onclick="formSubmit()" type="button" value="Add Row" />
   
   <input onclick="google.script.host.close()" type="button" value="Exit" />
   </form>
  <script type="text/javascript">
        function formSubmit() {
            google.script.run.getValuesFromForm(document.forms[0]);
            
        }
    </script>

function demoHtmlServices() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(),
      html = HtmlService.createHtmlOutputFromFile('Index').setWidth(200).setHeight(550);
  ss.show(html);
}


function getValuesFromForm(form){
 var NrItem = form.NrItem,
    date = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
      sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i in sheets) {
  for (var j in form.choices) {
    if (form.choices[j] == sheets[i].getName()) {
      sheets[i].appendRow([date, NrItem]);
    }
  }
}
}

我还有另外一个问题,如何发送电子邮件并在表单中插入信息?当我有关于“Nr项目”和“工作表编号”的信息时,我想发送关于信息“Nr项目”的电子邮件,并且将插入工作表信息。

enter image description here

此致

1 个答案:

答案 0 :(得分:0)

尝试选择一张表而不是全部迭代。例如:

function getValuesFromForm(form){
 var NrItem = form.NrItem;
 var date = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
 var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; // Added [0] but you could select any sheet
//removed this line: for (var i in sheets) {
  for (var j in form.choices) {
    if (form.choices[j] == sheets.getName()) { // removed the [i]
      sheets.appendRow([date, NrItem]); // removed the [i]
    }
  }
//removed this line: }
}

您也可以使用.getSheets()[0]而不是.getSheetByName(name)。 让我知道它是否适合你!