I store some data to tinyDB with CSV format
I Have a script in Google Sheet to import data.
The problem is that when i call a list view to read my DB alla records display Fine. But when i try to import to Sheet not all records importing. I get succesful message from my script but in Sheet i have missing entries if i repeate the send they upload random entries for example first time 1,2,4,5 next 1,4,3,6 etc.
I try to (Set Web1 url to:) inside and outside the loop with same results.
I dont wont first to create a csv file so please telle me you thing any solution. My scipt to Google sheet is this but i dont think have any issue.
function doPost(e) {
try {
// Get the spreadsheet and sheet
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheets()[0];
// Get the CSV data from the request body
const csvData = e.postData.contents;
// Parse the CSV data
const data = Utilities.parseCsv(csvData);
// Append each row to the sheet
for (const row of data) {
sheet.appendRow(row);
}
return ContentService.createTextOutput("Data imported successfully!");
} catch (error) {
Logger.log(error);
return ContentService.createTextOutput("An error occurred during import.");
}
}
And when i get it in this form in my list with coma delimited as expexted. "data1","dateformat","ID","",,, etc. When i receved it to my spread seed it desplayd correctly but i dont get all my data. i get all the record as suppose to be ine any cell but not all records.
If i try again of cource i have duplicate's and some new records that is not dysplay before.
Ok, I see the issue. Your current method is causing a log jam, which is why some data does not get through. Also, you are already saving your data as an AI2 list of sorts...Give me some time to come back with a couple of solutions for you
You will see that the web component waits for a response from the apps script before send the next record.
You would use a script like this:
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];
//POST function appends all data sent as parameters to the next row on the sheet, in the order sent
function doPost(e) {
var data = JSON.parse(e.postData.contents);
sh.appendRow(data);
return ContentService.createTextOutput(data);
}
To send all the data in one POST (more efficient), you could do this:
function doPost(e) {
var data = JSON.parse(e.postData.contents) ;
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('Sheet1');
for (var i=0;i<data.length;i++) {
sh.appendRow(data[i]);
}
return ContentService.createTextOutput("Success") ;
}