Transferring Google Sheet data to and from a txt file?

I need to save the contents of a Google Sheet to a file and then reload from the file (.txt) into a list structured as a list of lists (as Google Sheet needs). For now I do it by manipulating the text and recreating the list, but I wonder if there is a faster system.

Simply load the google sheet directly to a list of lists.

No sorry, I need first to save it to a phone file. After that reload it to a list of lists.

Are you saving as csv data (rows and columns), or some other...?

Spreadsheet readsheet, and than save the SheetData to file .txt

If I understand correctly, you want to fetch the data from a google sheet and save it to a file, then always read the data from that file into your app as a list ? (so this last activity could happen for both online and offline use)

How often will you want to read the data from the google sheet ?
Do you need all the data in your app, or do you want to query it for specific records ?

At start my app it needs to load 6 sheets to work. Than needs to prepare some of them for use. This takes about 20 seconds ...too much. So now I save the sheets as they are (when exit the app) to a file. At start if the app find the file existing, loads from them and takes 3 seconds. When need it will reload from Google Sheets.

This seems reasonable.

You need all the data from all the sheets, all at once, all the time ?

Why do you need to save the data to a file, instead of to, for example, tinydb, keeping it as data...?

No, in reality there are only 4 files saved on the phone regarding only 2 sheets.
For example, if I have the following text in a .txt file:
[["bla,111"],["bla,222"],["bla",333]]
can you show me how to get this data into a list of lists ready to be saved into a sheet?

Something like this:

image

Here to raw responseContent (data as csv table, as displayed in label1) is saved to file1.txt

When file1.txt is read, the text is converted to an AI2 list, as displayed in label2

What you get with Web1 is different from what I get when reading with Spreadsheet. With Spreadsheet I get the format 'list of lists' in the .txt file. And this text is not accepted by 'list from csv table text'. I had already tried with that block after reading other suggestions from the community.

Are you using the Spreadsheet component, which may return the data "pre-prepared" for Ai2 lists ? (Just tested, yes it does)

before storing you have to convert the list of lists into a csv table using the list to csv table block

Can you explain, why you need the data additionally stored in a file? You already have them as list of lists in the app and could store that list of lists in TinyDB

Taifun

The only reason was to read the contents of the files from the PC.
I tried with TinyDB and it seems to work very well even without the 'list from csv table text' block. I attach the blocks. Is it OK?

As mentioned with TinyDB it is OK.
However, I still have the question of why the same operation doesn't work with a file on a phone.
Can you tell me what block should I put in the WriteRange?

You have to convert the list of lists into a csv table before storing (list to csv table) and after reading convert it back (list from csv table)

The list is not a list anymore after storing, it only looks like a list...

Also you can't use the spreadsheet component for reading parts of a file, you only can read the complete file

Set myList to list from csv table text
Taifun

OK!! Now it works. Another step in my AI2 learning process. Thank you.

1 Like