问题
I try create in google sheet form to inserting data into several sheets at once. When you open the form you have field for entering some values and Checkbox where you can select a sheet to enter data. I have a question, how can I make data to be written to several sheets at once? Below my code.
<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="something1" /> sheet1
<br>
<br>
<input name="choices" type="checkbox" value="something2" /> sheet2
<br>
<br>
<input name="choices" type="checkbox" value="something3" /> 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);
}
//getValuesFromForm
function getValuesFromForm(form){
var NrItem = form.NrItem,
date = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.appendRow([date, NrItem]);
}
回答1:
How about following modification?
In order to retrieve values from input
, it uses this.parentNode
for formSubmit()
. By this, at getValuesFromForm(form)
, NrItem
can be retrieved by form.NrItem
.
Modified script :
This is the modification for the HTML file.
.
.
.
<input onclick="formSubmit(this.parentNode)" type="button" value="Add Row" />
<input onclick="google.script.host.close()" type="button" value="Exit" />
</form>
<script type="text/javascript">
function formSubmit(e) {
google.script.run.getValuesFromForm(e);
}
</script>
Note :
When sampletext
was inputted to the text box and the checkboxes of sheet1 and sheet2 are checked, this.parentNode
becomes
{"NrItem":"sampletext","choices":["something1","something2"]}
If I misunderstand your question, I'm sorry.
Edit
If the sheet names of Sheet1, Sheet2 and Sheet3 are constant, you can use following sample.
Modified HTML :
<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>
Modified GAS :
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]);
}
}
}
If this was not the sample you want, feel free to tell me.
来源:https://stackoverflow.com/questions/45892895/inserting-data-into-different-sheets-in-google-sheet