1
votes

I have created script with add information into several sheets. But it don't work when I want add information in only one sheet. What should I change in this script? This is link to my previous topic: 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]);
    }
  }
}
}

And I have another question to this, how can I send email with information insert in form? When I have information about "Nr item" and "sheets numbers" I want send email about information "Nr item" and in with sheet information will be insert.

enter image description here

Regards

1
Post your code, what have you tried?Parag Jadhav
Also please give a more detailed description than "it doesn't work"Robin Gertenbach

1 Answers

0
votes

Try to select a sheet instead of iterating them all. For example:

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: }
}

Instead of .getSheets()[0], you could also use .getSheetByName(name). Let me know if it works for you!