-
-
Save bainternet/4b539b00a4bd7490ac3809d7ff86bd14 to your computer and use it in GitHub Desktop.
/** | |
* Google app-script to utilise Elementor Pro From webhook. | |
* | |
* In order to enable this script, follow these steps: | |
* | |
* From your Google Sheet, from the "Exstensions" menu select "App Script"… | |
* Paste the script from this gist into the script code editor and hit Save. | |
* From the "Deploy" menu, select Deploy as web app… | |
* Choose to execute the app as yourself, and allow Anyone, even anonymous to execute the script. (Note: depending on your Google Apps instance, this option may not be available. You will need to contact your Google Apps administrator, or else use a Gmail account.) | |
* Now click Deploy. You may be asked to review permissions now. | |
* The URL that you get will be the webhook that you can use in your elementor form, You can test this webhook in your browser first by pasting it. | |
* It will say "Yepp this is the webhook URL, request received". | |
* Last all you have to do is set up and Elementor Pro Form with a form name and Webhook action pointing to the URL from above. | |
* | |
* Update: 09/06/2022 | |
* - Name the sheet: you can now add a field (could be hidden) to your form labeled "e_gs_SheetName" and set the defualt value to the name of the sheet you want to use. | |
* - Set the Order: you can now add a form field (hidden) labeled "e_gs_order" and set its defualt value to the names of the columns you want to seperated by comma in the order you want, any other colum not in that list will be added after. | |
* - Exclude Columns: you can now add a field (could be hidden) labeled "e_gs_exclude" and set its value to the names of the columns you want to exclude seperated by comma. | |
*/ | |
// Change to true to enable email notifications | |
let emailNotification = false; | |
let emailAddress = "Change_to_your_Email"; | |
// DO NOT EDIT THESE NEXT PARAMS | |
let isNewSheet = false; | |
let postedData = []; | |
const EXCLUDE_PROPERTY = 'e_gs_exclude'; | |
const ORDER_PROPERTY = 'e_gs_order'; | |
const SHEET_NAME_PROPERTY = 'e_gs_SheetName'; | |
/** | |
* this is a function that fires when the webapp receives a GET request | |
* Not used but required. | |
*/ | |
function doGet( e ) { | |
return HtmlService.createHtmlOutput( "Yepp this is the webhook URL, request received" ); | |
} | |
// Webhook Receiver - triggered with form webhook to published App URL. | |
function doPost( e ) { | |
let params = JSON.stringify( e.parameter ); | |
params = JSON.parse( params ); | |
postedData = params; | |
insertToSheet( params ); | |
// HTTP Response | |
return HtmlService.createHtmlOutput( "post request received" ); | |
} | |
/** | |
* flattenObject | |
* Flattens a nested object for easier use with a spreadsheet | |
* @param ob | |
* @returns {{}} | |
*/ | |
const flattenObject = ( ob ) => { | |
let toReturn = {}; | |
for ( let i in ob ) { | |
if ( ! ob.hasOwnProperty( i ) ) { | |
continue; | |
} | |
if ( ( typeof ob[ i ] ) !== 'object' ) { | |
toReturn[ i ] = ob[ i ]; | |
continue; | |
} | |
let flatObject = flattenObject( ob[ i ] ); | |
for ( let x in flatObject ) { | |
if ( ! flatObject.hasOwnProperty( x ) ) { | |
continue; | |
} | |
toReturn[ i + '.' + x ] = flatObject[ x ]; | |
} | |
} | |
return toReturn; | |
} | |
/** | |
* getHeaders | |
* normalize headers | |
* @param formSheet | |
* @param keys | |
* @returns {*[]} | |
*/ | |
const getHeaders = ( formSheet, keys ) => { | |
let headers = []; | |
// retrieve existing headers | |
if ( ! isNewSheet ) { | |
headers = formSheet.getRange( 1, 1, 1, formSheet.getLastColumn() ).getValues()[0]; | |
} | |
const newHeaders = keys.filter( h => ! headers.includes( h ) ); | |
headers = [ ...headers, ...newHeaders ]; | |
// maybe set order | |
headers = getColumnsOrder( headers ); | |
// maybe exclude headers | |
headers = excludeColumns( headers ); | |
// filter out control columns | |
headers = headers.filter( header => ! [ EXCLUDE_PROPERTY, ORDER_PROPERTY, SHEET_NAME_PROPERTY ].includes( header ) ); | |
return headers; | |
}; | |
/** | |
* getValues | |
* normalize values | |
* @param headers | |
* @param flat | |
* @returns {*[]} | |
*/ | |
const getValues = ( headers, flat ) => { | |
const values = []; | |
// push values based on headers | |
headers.forEach( ( h ) => values.push( flat[ h ] ) ); | |
return values; | |
} | |
/** | |
* insertRowData | |
* inserts values to a given sheet at a given row | |
* @param sheet | |
* @param row | |
* @param values | |
* @param bold | |
*/ | |
const insertRowData = ( sheet, row, values, bold = false ) => { | |
const currentRow = sheet.getRange( row, 1, 1, values.length ); | |
currentRow.setValues( [ values ] ) | |
.setFontWeight( bold ? "bold" : "normal" ) | |
.setHorizontalAlignment( "center" ); | |
} | |
/** | |
* setHeaders | |
* Insert headers | |
* @param sheet | |
* @param values | |
*/ | |
const setHeaders = ( sheet, values ) => insertRowData( sheet, 1, values, true ); | |
/** | |
* setValues | |
* Insert Data into Sheet | |
* @param sheet | |
* @param values | |
*/ | |
const setValues = ( sheet, values ) => { | |
const lastRow = Math.max( sheet.getLastRow(), 1 ); | |
sheet.insertRowAfter( lastRow ); | |
insertRowData( sheet, lastRow + 1, values ); | |
} | |
/** | |
* getFormSheet | |
* Find or create sheet for form | |
* @param sheetName | |
* @returns Sheet | |
*/ | |
const getFormSheet = ( sheetName ) => { | |
const activeSheet = SpreadsheetApp.getActiveSpreadsheet(); | |
// create sheet if needed | |
if ( activeSheet.getSheetByName( sheetName ) == null ) { | |
const formSheet = activeSheet.insertSheet(); | |
formSheet.setName( sheetName ); | |
isNewSheet = true; | |
} | |
return activeSheet.getSheetByName( sheetName ); | |
} | |
/** | |
* insertToSheet | |
* magic function where it all happens | |
* @param data | |
*/ | |
const insertToSheet = ( data ) => { | |
const flat = flattenObject( data ), | |
keys = Object.keys( flat ), | |
formSheet = getFormSheet( getSheetName( data ) ), | |
headers = getHeaders( formSheet, keys ), | |
values = getValues( headers, flat ); | |
setHeaders( formSheet, headers ); | |
setValues( formSheet, values ); | |
if ( emailNotification ) { | |
sendNotification( data, getSheetURL() ); | |
} | |
} | |
/** | |
* getSheetName | |
* get sheet name based on form field named "e_gs_SheetName" if exists or used form name | |
* @param data | |
* @returns string | |
*/ | |
const getSheetName = ( data ) => data[SHEET_NAME_PROPERTY] || data["form_name"]; | |
/** | |
* getSheetURL | |
* get sheet url as string | |
* @returns string | |
*/ | |
const getSheetURL = () => SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getUrl(); | |
/** | |
* stringToArray | |
* split and trim comma seperated string to array | |
* @param str | |
* @returns {*} | |
*/ | |
const stringToArray = ( str ) => str.split( "," ).map( el => el.trim() ); | |
/** | |
* getColumnsOrder | |
* used to set the columns order, set this by adding a form field (hidden) named "e_gs_order" | |
* and set its value to the names of the columns you want to seperated by comma in the order you want, | |
* any other colum not in that field will be added after | |
* @param data | |
* @param headers | |
* @returns {*} | |
*/ | |
const getColumnsOrder = ( headers ) => { | |
if ( ! postedData[ORDER_PROPERTY] ) { | |
return headers; | |
} | |
let sortingArr = stringToArray( postedData[ORDER_PROPERTY] ); | |
// filter out non existing headers | |
sortingArr = sortingArr.filter( h => headers.includes( h ) ); | |
// filterout sorted headers | |
headers = headers.filter( h => ! sortingArr.includes( h ) ); | |
return [ ...sortingArr, ...headers ]; | |
} | |
/** | |
* excludeColumns | |
* used to exclude columns, set this by adding a form field (hidden) named "e_gs_exclude" | |
* and set its value to the names of the columns you want to exclude seperated by comma | |
* @param data | |
* @param headers | |
* @returns {*} | |
*/ | |
const excludeColumns = ( headers ) => { | |
if ( ! postedData[EXCLUDE_PROPERTY] ) { | |
return headers; | |
} | |
const columnsToExclude = stringToArray( postedData[EXCLUDE_PROPERTY] ); | |
return headers.filter( header => ! columnsToExclude.includes( header ) ); | |
} | |
/** | |
* sendNotification | |
* send email notification if enabled | |
* @param data | |
* @param url | |
*/ | |
const sendNotification = ( data, url ) => { | |
MailApp.sendEmail( | |
emailAddress, | |
"A new Elementor Pro Forms submission has been inserted to your sheet", // mail subject | |
`A new submission has been received via ${data['form_name']} form and inserted into your Google sheet at: ${url}`, //mail body | |
{ | |
name: 'Automatic Emailer Script' | |
} | |
); | |
}; |
Did not work for me. Did exactly as instructed here.
- Created new google sheet file.
- Tools>Script editor
- paste the code and save.
- deploy>new deployment
- deploy> allow authorization
- copy web app link and paste in elementor form.
After submitting the form there is no update in google sheets.
Can anyone help?
@Trushar10,
It creates a new tab.
@Trushar10,
It creates a new tab.
It doesn't create any tab
Unfortunately it doesn't work for me.
Script function not found: doGet
Doesn't work for me either, followed every step and when I paste the URL in the browser I get the response "Yepp this is the webhook URL, request received", but nothing happens in the sheet.
I was getting Script function not found: doGet but it's funny that if I make a second one (select New Deployment again), then it works for some reason! And yes it creates a new sheet tab in the spreadsheet file, so I just delete the first blank one.
This is really great, but I wonder if there's a way to make the new row at the top, so newest submissions are first, as the list starts to get really long and there's a lot of scrolling. I know I could just re-order the sheet by date, but the client would have to remember to do that everyday.
I am struggling to get this to work. The script URL gets the correct results from the address bar - but the form data is not being passed to my google sheet. Any ideas?
Works perfect for me! Thank you very much for sharing!!!
Questions: @bainternet
- Can I sort the columns in any way?
- Is it possible to exclude certain form fields? (in my case: field_18b53fb)
- Is it possible to rename certain form fields? (for example: "very very very long form field title" > "short title" )
Hi, @bainternet
I have a problem. The data sent to the spreadsheet is not in the same order as that of Form. Has anyone ever experienced this?
Hi, @bainternet
I have a problem. The data sent to the spreadsheet is not in the same order as that of Form. Has anyone ever experienced this?
Yes, same here.
I wonder how to keep the order of the form…
1 time Simple solution: Just reorder the columns in the spreadsheet manually.
Too bad this doesn't work for me. did anyone get it to work and can share the new code?
@Karenelle The code works!
Not sure how to help. Github doesn’t provide a private message service or something, right?
Thanks :)) @ariako
I managed to find a other code that worked for me. so I'm fine.. thank you for the comment :)
@kerenelle Can you share the link?
Unfortunately it doesn't work for me.
Script function not found: doGet
Same here :(
Script function not found: doGet
working perfectly here thanks!
I would like to know if it is possible to delete the form_id, form_name and some other form fields if necessary
Hello guys, I'm getting a doGet error, the form is being submitted successfully, but the script is not working.
what can I do to avoid this error?
Hello guys, I'm getting a doGet error, the form is being submitted successfully, but the script is not working.
what can I do to avoid this error?
Please make sure this option is unchecked and try again
I love this script, I have used it a bunch of times and it works like a charm!!
Now that I added Piotnet forms to my web - in order to create more complex forms - the webhook is no longer pulling data - it creates new empty sheets and that's basically it - is there a way or an additional code to make the webhook work? piotnet forms + elementor pro + google sheets.
Thanksss!!
doesn't work on me :( the google sheet is blank.
how can we get a notification in every new submission?
Also me not working please any idea?
Also me not working please any idea?
I just used it following the exact directions from the script and it works like a charm
It worked very well for me. Awsome job. thank you
how can i sort the headers in the order they appear in the form? headers are not loaded in their original order.
Thanks for the solution
how can i sort the headers in the order they appear in the form? headers are not loaded in their original order.
Thanks for the solution
Have you find any solution for sorting the order?
Hi all, :)
The script is live and kicking, I just refactored the code with a more modern version and added a few enhancements:
- Name the sheet: You can now add a field (could be hidden) to your form labeled
e_gs_SheetName
and set the default value to the name of the sheet you want to use. - Set the Order: you can now add a form field (hidden) labeled
e_gs_order
and set its default value to the names of the columns you want to separated by comma in the order you want, any other column not in that list will be added after. - Exclude Columns: you can now add a field (could be hidden) labeled
e_gs_exclude
and set its default value to the names of the columns you want to exclude separated by comma.
Other than that you can exclude columns by simply hiding them in the sheet and you can set the order once manually via the sheets UI.
Enjoy.
How to ordering column in spreadsheet? I have try & not work.
Hi,
Thanks for the code is working well.
But when I use a dropdown/radio type of form, the data is not sent to the google sheet.
just wondering if you have version that cater for this type of form!