Problem with Export TinyDB to Google Sheet

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.");
  }
}


I see this before i post but is not a solution for tyniDB unfortunately.
As i sayd i dont wont to extract data from csv file.

You do not have to create a csv file.

Convert your "csv data" from tinydb to an AI2 list and use that (it should be a stringified JSON array when sent to the google apps script)

It may help to share some example data, in case there are any formatting issues.

Ok my data are generated fro here.


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

  1. You should store your data to tinydb like so:

image

no need for the additional "make a list"

a single record: ["event1", "10:40", "Bob"]

  1. To send data records one by one, you could use this approach:

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);  
}
  1. To send all the data in one POST (more efficient), you could do this:

all records: [["event1", "10:40", "Bob"], ["event2", "13:50", "Sue"], ["event3", "09:30", "Jim"]]

and a script like 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") ;
}
2 Likes

I replicate the second method to make in one post and working perfect. Thank you for your time and for your knowledge
:+1: :ok_hand:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.