Last active
January 14, 2024 17:30
-
-
Save jonathasborges1/f93f0d1a461bb64a2e07050f7f2b032a to your computer and use it in GitHub Desktop.
Brincando com script.gs do Google App Script - a funcao doPost envia uma requisicao para google form e cadastra dados em um formulario google
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doGet(e){ | |
return ContentService.createTextOutput("Backend do formulario v0.0.2").setMimeType(ContentService.MimeType.TEXT); | |
} | |
function doPost(e) { | |
var URL_ACTION = 'https://docs.google.com/forms/u/0/d/e/ID_DO_SEU_GOOGLE_FORMS/formResponse'; | |
var EMAIL_ID = 'entry.20457302'; | |
var SUBJECT_ID = 'entry.59777055'; | |
var MESSAGE_ID = 'entry.20138404'; | |
try { | |
var data = JSON.parse(e.postData.contents); | |
if (!data.email || !data.subject || !data.message) { | |
console.error('Dados inválidos recebidos:', data); | |
return buildErrorResponse(400, 'Dados inválidos recebidos'); | |
} | |
var payload = {}; | |
payload[EMAIL_ID] = data.email; | |
payload[SUBJECT_ID] = data.subject; | |
payload[MESSAGE_ID] = data.message; | |
console.log("Enviando dados para o Google Forms...") | |
var response = sendPostRequest(URL_ACTION, payload); | |
console.log("Dados Enviados com sucesso.") | |
var statusCode = response.getResponseCode(); | |
console.log("Debug do statusCode: ",statusCode); | |
if (statusCode === 200) { | |
return buildSuccessResponse(); | |
} else { | |
console.error('Erro na requisição. Código de status:', statusCode); | |
var responseText = response.getContentText(); | |
return buildErrorResponse(statusCode, 'Erro na requisição. ' + responseText); | |
} | |
} catch (error) { | |
console.error("A funcao doPost nao foi concluida, motivo: ", error); | |
return buildErrorResponse(500, 'Erro no processamento da requisição'); | |
} | |
} | |
function sendPostRequest(url, payload) { | |
// Converte o objeto payload para uma string no formato x-www-form-urlencoded | |
var payloadString = Object.keys(payload).map(function(key) { | |
return encodeURIComponent(key) + '=' + encodeURIComponent(payload[key]); | |
}).join('&'); | |
console.log("debug Payload to form-urlencoded: ",payloadString); | |
return UrlFetchApp.fetch(url, { | |
method: 'post', | |
contentType: 'application/x-www-form-urlencoded', | |
payload: payloadString | |
}); | |
} | |
function buildSuccessResponse() { | |
var responseObject = { | |
message: "Cadastro feito com sucesso", | |
status: 200 | |
}; | |
return createTextOutput(responseObject); | |
} | |
function buildErrorResponse(statusCode, errorMessage) { | |
var responseObject = { | |
message: errorMessage || "Não foi possível concluir a operação. Tente mais tarde.", | |
status: statusCode | |
}; | |
return createTextOutput(responseObject); | |
} | |
function createTextOutput(obj) { | |
var responseString = JSON.stringify(obj); | |
return ContentService.createTextOutput(responseString).setMimeType(ContentService.MimeType.TEXT); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment