Hello there,
I am creating a google site to help connect donors to local restaurants and charities to help keep our local restaurants afloat during this difficult time as well as provide meal vouchers to the many people who have been laid off due to the current situation using local charity participation. The idea is that a donor calls a restaurant and says they want to purchase a # of meal vouchers. The restaurant takes the payment, fills out a google form, with the # of vouchers and the charity they want to support. The form will then populate a sheet with the information and a doc is created for each voucher.
Issue: I am using Document Studio to do the auto doc generation and it was working fine when I input one record from the form at a time. However, sometimes people will want to order multiple vouchers and we don't want to have to enter into the form multiple times. The idea is to use a '# of Vouchers' variable from the form and enter that number of rows into the sheet so we can create individual vouchers to be passed out. Our script is acting 'wonky' and the first entry works fine (Say, Bob orders 5 vouchers. The script creates 5 rows as expected.) The problem is then when we go in and fill out another form and say Fred orders 6 vouchers. The form puts the new record (Fred) under the originally created record for Bob and the script adds new records for Bob and not Fred. Screen shots from the results. First is after Bob's original form submit and second is after Fred's form commit.
Here is our script:
function populateVouchers() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var lastRow = data.length -1;
var numVouchers = data[lastRow][7] - 1;
while (numVouchers > 0) {
numVouchers--;
sheet.appendRow([data[lastRow][0],
,// Skip the ID Column. It is populated by a formula
data[lastRow][2],
data[lastRow][3],
data[lastRow][4],
data[lastRow][5],
data[lastRow][6],
data[lastRow][7],
data[lastRow][8]]);
}
}
Can anyone help? It is probably clear that I am NOT a developer. :) I am technical and just trying to do something simple quickly to support our community before any more of our small local restaurants close. (They are all only doing pickup/delivery.) I appreciate any guidance you can provide!!
Chris