Here is what I’ve settled on
Although the loop (above) worked. It was difficult to work with the output. Depending on whether the guest RSVPs from the webpage or the mobile app there is some different behaviour.
If the guest doesn’t leave a comment then the phone number index moves, same for additional guests. The only way to guarantee that I’m getting right result is to use findIndex to return the objects index number. I can then assign the variable with the right value using rsvp[indexName].value. Some things never move, such as firstName and lastName, so I didn’t need to use findIndex.
You’ll also notice the default values for guestOne and guestTwo || " ". This is to overcome the differences between the behaviour of the Web vs App and the information that they return.
Because the comment is optional I’ve used a push (values.push(comment) to tac that on to the end of [values], if the comment is present.
This should work for any event, provided that the custom fields don’t get out of hand, then you might need to tweak it a bit.
There is probably a nicer, neater way butt hopefully someone finds this useful or at least helpful.
import { appendValuesWrapper } from ‘backend/googlesheet-wrapper.jsw’;
export function wixEvents_onRsvpCreated(event) {
const eventId = event.eventId;
const rsvp = event.rsvpForm.inputValues;
console.log(rsvp);
const bb = rsvp[0].value;
const formemail = rsvp[1].value;
const formlastName = rsvp[2].value;
const formfirstName = rsvp[3].value;
let addGuests = "0"
let guestNameOne =" "
let guestNameTwo = " "
let addGuestIdx=rsvp.findIndex(function(ag){
return ag.InputName === "additionalGuests";
})
if(addGuestIdx>0){
addGuests = rsvp[addGuestIdx].value;
}
let guestNamesIdx = rsvp.findIndex(function(gn){
return gn.inputName ==="guestNames"
})
if (guestNamesIdx>0){
guestNameOne=rsvp[guestNamesIdx].values[0] || " ";
console.log(guestNameOne);
guestNameTwo=rsvp[guestNamesIdx].values[1] || "";
}
let commentIdx = rsvp.findIndex(function(cm){
return cm.inputName === "comment";
})
let phoneIdx = rsvp.findIndex(function(ph){
return ph.inputName === "phone";
})
const phone = rsvp[phoneIdx].value;
const values =[formfirstName, formlastName, formemail, phone, addGuests, bb, guestNameOne, guestNameTwo]
if(commentIdx>0){
let comment = rsvp[commentIdx].value;
values.push(comment) }
const res = appendValuesWrapper(values);
}