I just want make my react send and receive data for a private sheets, using some credential for this if necessary, my project is a site with interesting forms for workers and my boss want see these answers in google sheets. I already tried this:
- Sheets API using Node Js, but i can't put Node in my project and dont know how to use this.
- Sheets API using JS on React but i take a lot of strange libraries to make the #1 work, not happen.
- Google Apps Script, my last one attempt, i make one function to take data, transform this data on JSON file and return that. All rigth with this, i guess, but when i try use this function on react a wild bug appears: CORS policy.
Error:
localhost/:1
Access to XMLHttpRequest at 'https://script.google.com/macros/s/AKfycbxuz7xwcOu77AqEsAnnOS0MeQ2qu5D47ZnGRhMS3su_OegiNeyyFdoY3cK3svybyBZb/exec' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
My JS code:
const obj = {
redirect: "follow",
method: "POST",
headers: {
'Access-Control-Allow-Origin': '*',
"Content-Type": "text/plain;charset=utf-8",
},
};
const url = "https://script.google.com/macros/s/AKfycbxuz7xwcOu77AqEsAnnOS0MeQ2qu5D47ZnGRhMS3su_OegiNeyyFdoY3cK3svybyBZb/exec";
axios.get(url, obj)
.then((response) => {
console.log(response);});
My Apps Script code:
function doGet(e) {
const ss = SpreadsheetApp.openById("1Qo78FwRfUBE20q3uJO4me-I30yd9IO64_cf48OER2Qk").getSheetByName("pag1");
const values = ss.getDataRange().getValues();
//Headers
const headers = values[0];
//Values
const info = values.slice(1);
const holder = [];
info.forEach((ele, ind)=>{
const temp = {};
headers.forEach((header,index) =>{
header = header.toLowerCase();
temp[header] = ele[index];
});
holder.push(temp);
});
Logger.log(holder);
const output = JSON.stringify({
status: true,
data: holder
});
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON);
}
If i put code:"no-cors" the res no have my data. What am i doing wrong?