简体   繁体   中英

Script in spreadsheet to save sheet as csv and upload to cloud storage

I have a script that everyday gets data from Google analytics and then I delete the sheet, so next day I can download it again.

What I need is to save that sheet as .csv and upload it to a cloud storage everyday, any ideas about how can I do that ?

Thanks a lot!

To download your file as csv you can make use of this thread , which basically consists on using the following:

https://docs.google.com/spreadsheets/d/{DOCID}/gviz/tq?tqx=out:csv&sheet={sheet_name}

Replacing {DOCID} with your Document ID and {sheet_name} with the name of the sheet. If you only have one sheet, there is a more straightforward way:

https://docs.google.com/spreadsheets/d/{DOCID}/export?format=csv

As already mentioned by @jpaugh, to upload the file to cloud storage you can use the official docs, specifically here : Where it is explained how to upload an object to a bucket using any of the available options, which are:

  1. Using the Console
  2. Using gsutil command
  3. Using any of the client libraries: C#,GO,Java,Node.js, PHP, Python or Ruby
  4. Using any of the REST APIS (JSON or XML).

Doesn't get any simpler than using Pandas :

def build_sheet_url(doc_id, sheet_id):
    return f'https://docs.google.com/spreadsheets/d/{doc_id}/export?format=csv&gid={sheet_id}'

def write_df_to_gs(df, gs_key):
    df.to_csv(gs_key)

doc_id = 'DOC_ID'
sheet_id = 'SHEET_ID'
sheet_url = build_sheet_url(doc_id, sheet_id)
df = pd.read_csv(sheet_url)
gs_key = 'GS_KEY'
write_df_to_gs(df, gs_key)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM