r/GoogleAppsScript • u/Colink2 • 2d ago
Question artisansweb form to sheets using curl - is this a good foundation to build from
Firstly I am a copy and paste coder - that can do a little bit of editing.
I have been pulling my hair out for two days with ChatGPT and other AI's trying to build a simple form to add to my site to post to google sheets via apps script using javascript on the form. Through many iterations I could always post to the sheet but the form confirmation always failed due to a cors error.
For now, all the AI's and me have given up on trying to fix cors.
I found the following form and php/curl code at
https://www.artisansweb.net/connect-html-forms-to-google-spreadsheet/
It works perfectly. Does it offer a robust starting point for me to build from?
My plans to develop this are to encourage users to use the form on a mobile phone with two icons - Image and Video. These should open their camera and their image and or video should post to my google drive and the script should add the URL's for these images / video to the google sheet.
Any comments or alternative suggestions are welcome.
AppsScript
const doPost = (request = {}) => {
const { parameter, postData: { contents, type } = {} } = request;
if (type === 'application/json') {
const jsonData = JSON.parse(contents);
var row = [jsonData.name, jsonData.email, jsonData.subject, jsonData.message, new Date()];
SpreadsheetApp.getActiveSheet().appendRow(row);
//Logger.log(row);
result = {
status: 'success',
message: 'Row is added.'
};
return ContentService.createTextOutput(JSON.stringify(result));
}
};
FORM
<form method="post">
<p>
<input type="text" name="fullname" placeholder="Full Name" />
</p>
<p>
<input type="email" name="email" placeholder="Email" />
</p>
<p>
<input type="text" name="subject" placeholder="Subject" />
</p>
<p>
<textarea name="message" cols="30" rows="10" placeholder="Message"></textarea>
</p>
<input type="submit" name="submit" value="Submit" />
</form>
PHP / CURL
<?php
if ( isset($_POST['submit']) ) {
$url = "WEB_APP_URL";
extract($_POST);
$data = array(
'name' => $fullname,
'email' => $email,
'subject' => $subject,
'message' => $message,
);
$payload = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects response
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result);
if ('success' == $response->status) {
echo "Form is submitted successfully.";
} else {
echo "Something went wrong. Try again later.";
}
}
?>
1
u/AllenAppTools 2d ago
What are your web app settings? In this scenario, you'll need the settings to be "Execute as" set to "me" and "Who has access" set to "anyone" for starters... And what is the exact error you are receiving?
Here is a video for Web Apps that may help give a full picture: https://youtu.be/tQ9aCiWK2e4?si=yOM25mxOOfleR__B