For use with ApostropheCMS 2.x. Requires Node v8 and apostrophe-forms v1.x
.
This module adds an additional form submission option to Apostrophe Forms. It allows website managers to configure individual forms to submit to a specific Google spreadsheet. The sheet must exist, but does not necessarily need to be set up with column headings before use. If you wish to set column headings directly (e.g., to set the order of columns yourself), those column headings must exactly match the field name from the Apostrophe form.
lib/modules/apostrophe-forms
directory in your Apostrophe project as credentials.json
.
.gitignore file
(for Git) and put it directly on your production server. Alternately you can provide the file as JSON in an environment variable named GOOGLE_APPLICATION_CREDENTIALS
.Please note that you must not add any empty, unlabeled columns to the spreadsheet once submissions begin. Due to the rules of Google's spreadsheet API the gap will be considered as the start of a "new table" and newly appended rows will start at that column, which is probably not what you want. If this does happen, move the data over and add a header to the empty column.
Enabled the module in your Apostrophe app.js
file with other modules.
// in app.js
modules: {
// ...,
'apostrophe-forms': {},
// ... Other Apostrophe Forms modules and configurations
'apostrophe-forms-submit-google': {},
}
"Date Submitted" and "Time Submitted" columns are added automatically. These are always in UTC (Coordinated Universal Time).
If you wish to modify the submitted data just before it goes to Google, for instance to add a new property, you can catch the apostrophe-forms-submit-google:beforeSubmit
event. Let's say we want to create a "unique key" column based on the date submitted, time submitted, and an email field in the submission:
// In the index.js of your own module
module.exports = {
construct: function(self, options) {
self.on('apostrophe-forms-submit-google:beforeSubmit', 'addUniqueKey', async (req, form, data) => {
data['Unique Key'] = data['Date Submitted'] + data['Time Submitted'] + data.email;
});
}
};
The submitted spreadsheet rows will now include the additional column.
This module sends data to Google Sheets "as entered," i.e. as if the it were typed by the user in Google Sheets. In most cases this does good things: dates are detected as dates, times as times, numbers as numbers, etc.
However in certain cases, the results may be surprising. For instance, a phone number with a leading 0
and no spaces or punctuation will lose its leading 0
because this is the standard behavior of Google Sheets when it believes it has detected a number. Google does not store the zero in this situation, it is truly gone.
Fortunately you can correct this by formatting the column correctly in Google Sheets. Open the sheet, select the column that will contain phone numbers, and select "Format -> Number -> Plain text". Leading zeroes will not be removed from future submissions.