我有一個註冊表,它收集用戶想要註冊事件的日期。使用複選框,用戶可以選擇多個日期,然後將這些日期添加到formObject
中的數組中。要將這些值寫入Google工作表,我會檢查日期數組的長度,然後循環,以便將單個行添加到電子表格中。從表單提交數組到數組中提交
我很難迫使class
鍵總是是一個數組,即使它是一個類。如果只選擇一個類,那麼length
屬性是字符串的長度,用單個字符寫入9或10行數據。
表
<form id="classSignup" onsubmit="handleFormSubmit(this)">
<label for="first">First Name</label>
<input id="first" name="first" type="text" />
<label for="last">Last Name</label>
<input id="last" name="last" type="text" />
<input type='checkbox' name='class' value='date1'>Date 1</input>
<input type='checkbox' name='class' value='date2'>Date 2</input>
<input type="submit" value="Register" />
</form>
formObject
{last=lastName, first=firstName, class=[dateArray]}
recordData
function recordData(formObject) {
try {
var ss = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
var sheet = ss.getSheetByName("allRegs");
var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow()+1;
var rows = [ ];
// check the formObject structure
Logger.log(formObject);
// Open the class array
for(var i=0; i<formObject.class.length; i++) {
// set the date with the current item
var date = formObject.class[i];
Logger.log(date);
var row = [new Date(), formObject[headers[1]], formObject[headers[2]], formObject[headers[3]], formObject[headers[4]], formObject.class[i]]
rows.push(row);
} // close the rows loop
// write the rows array to the spreadsheet
}
以上,如果date1
和date2
被選中,則返回一個數組,一切工作正常:
{ first=firstName, last=lastName, class=[date1, date2] }
當選定一個日期,也沒有數組,只是一個字符串在formObject
:
{ first=firstName, last=lastName, class=date1 }
如何我強迫任何選中的類被推入數組中?
哪裏是填充'formObject'的代碼?爲什麼不總是將'class'作爲數組傳遞('class = [date1]')? – billyonecan
「formObject」是使用Google Apps腳本功能構建的。我試圖用類作爲數組返回表單,但它被視爲一個字符串,而不是數組。 – Brian
你可以在'for'循環之前測試'typeof formObject.class',並且如果它的一個字符串使它成爲一個包含字符串 – billyonecan