Reputation: 95
So I've read just about every article and thread I could about getting this to work and it seems to be a common issue with a varying range of causes. I couldn't find the solution so I'm hoping you guys can help.
I published the script and got the typical (Script function not found: doGet).
Here's the script and html script code. I've uploaded the new published link to the html file. I was sure to make a "New" version instead of updating the old version. I'm assuming there's some syntax somewhere that I haven't called out properly. I just don't know what or where. Hoping you can help.
Here's the script code:
var sheetName = 'Sheet1'
var scriptProp = PropertiesService.getScriptProperties()
function intialSetup () {
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
scriptProp.setProperty('key', activeSpreadsheet.getId())
}
function doPost (e) {
var lock = LockService.getScriptLock()
lock.tryLock(10000)
try {
var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
var sheet = doc.getSheetByName(sheetName)
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
var nextRow = sheet.getLastRow() + 1
var newRow = headers.map(function(header) {
return header === 'timestamp' ? new Date() : e.parameter[header]
})
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
.setMimeType(ContentService.MimeType.JSON)
}
catch (e) {
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
.setMimeType(ContentService.MimeType.JSON)
}
finally {
lock.releaseLock()
}
}
Here's the script inside the HTML code
<script>
const scriptURL = 'https://script.google.com/macros/s/AKfycbwzy6qtuvcOpOrl9ELoy6lM9k5CFhZWGbb-XriD1SyofDeZ6tU/exec'
const form = document.forms['google-sheet']
form.addEventListener('submit', e => {
e.preventDefault()
fetch(scriptURL, { method: 'POST', body: new FormData(form)})
.then(response => alert("Thanks for Contacting us..! We Will Contact You Soon..."))
.catch(error => console.error('Error!', error.message))
})
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
Upvotes: 1
Views: 3662
Reputation: 38469
doGet
is required when the web app URL is opened by using a web browser. The code included in the question doesn't include this function.
Reference
Upvotes: 1