Google Forms脚本中如何从e.namedValues获取特定字段的值?
Hey there! Let's work through why your code isn't grabbing the "notificar" field value correctly—this is a super common quirk with Google Forms triggers, so I’ve got a few troubleshooting steps for you:
First, double-check the exact field name match
Google Forms uses the exact display text of your question as the key ine.namedValues, including capitalization, spaces, and even special characters. For example, if your form question is actually "Notificar" (capital N) or "notificar " (with a trailing space), your hardcoded"notificar"won’t line up. The easiest way to confirm is to log all the keys being passed in:const datos = e.namedValues || {}; console.log("All form fields received:", Object.keys(datos));Run a test submission, then check your script logs—this will show you the precise key name Google is sending, and you can adjust your code to match it exactly.
Simplify your value retrieval code
You don’t need to useObject.keys().find()here; you can directly access the field if you know the correct key. Once you’ve confirmed the right key name, you can simplify your code to:const datos = e.namedValues || {}; const correoDestino = datos["notificar"] ? datos["notificar"][0] : ""; console.log("Recipient email:", correoDestino);Remember, every value in
e.namedValuesis an array (even for single-line text fields) because some question types (like checkboxes) return multiple answers—so using[0]to get the first entry is correct for most single-input fields.Verify your trigger type
Make sure you’re using an "On form submit" trigger for your function. If you accidentally set up a time-driven trigger or another type, theeobject won’t have thenamedValuesproperty at all. You can check this in your script editor under Edit > Current project’s triggers—confirm the trigger is linked to your submission handler and set to fire on form submit.Check if the field was actually submitted
If your form uses section navigation or conditional logic, it’s possible the "notificar" question wasn’t visible to the user when they submitted. For example, if the question only appears based on a previous answer that wasn’t selected, Google won’t include it ine.namedValues. Test submitting the form while ensuring the "notificar" field is filled out every time to rule this out.
9 times out of 10, the issue is just a tiny mismatch in the field name—logging all the keys will immediately show you where the problem is. Give these steps a try, and you should be able to grab that email address without issues!
内容来源于stack exchange

