简体   繁体   English

如何将 gspread api 中的数据格式化为 Google 表格

[英]How can I format data from gspread api into Google Sheets

I am new to python (and programming in general).我是 python(和一般编程)的新手。 I am currently working on automating a data entry aspect of my job.我目前正在致力于自动化我工作的数据输入方面。 So far I have figured out how to sort out the data I want from a csv with pandas, and load it into the appropriate area of a google spreadsheet with the gspread api.到目前为止,我已经弄清楚如何从带有 pandas 的 csv 中整理出我想要的数据,并使用 gspread Z8A5DA52ED126447D359E70C05721A8 将其加载到谷歌电子表格的适当区域中。 My problem is that the csv data dumps into a single cell of the speadsheet.我的问题是 csv 数据转储到电子表格的单个单元格中。 How can I make have it enter across the row of cells?我怎样才能让它进入整个单元格行? I have tried every parameter of the.appendrow() function.我已经尝试了 .appendrow() function 的每个参数。

Thanks so much - Owen K非常感谢 - 欧文 K

import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name("/Users/macbook/Documents/Atom Projects/Sheets1/creds.json", scope)

client = gspread.authorize(creds)

import pandas as pd

df = pd.read_csv('/Users/macbook/Documents/Atom Projects/Sheets1/report.csv')#, index_col = '(Child) ASIN')

Luna = (df['(Child) ASIN'] == 'B0716JTGSX')
LU = df[Luna].to_csv()

print(LU)
bick4 = client.open("Test Sheet") .worksheet('bick 4')
bick4.append_row([LU])

I believe your goal as follows.我相信你的目标如下。

  • You want to put the values of LU = df[Luna].to_csv() to each cell in the Google Spreadsheet.您想将LU = df[Luna].to_csv()的值放入 Google 电子表格中的每个单元格。
  • You want to achieve this using gspread with python.您想使用带有 python 的 gspread 来实现此目的。
  • You have already been able to get and put values using Sheets API.您已经能够使用表格 API 获取和放置值。

For this, how about this answer?为此,这个答案怎么样?

Modification points:修改点:

  • In this case, a list is created from df[Luna] and the list is put to the Spreadsheet using gspread.在这种情况下,从df[Luna]创建一个列表,并使用 gspread 将该列表放入电子表格中。

I propose the following 2 patterns.我提出以下两种模式。

Pattern 1:模式一:

In this pattern, the value of df[Luna] is put to the Spreadsheet using the method of values_update .在此模式中,使用values_update方法将df[Luna]的值放入电子表格。

Sample script:示例脚本:

client = gspread.authorize(creds)
spreadsheet = client.open('Test Sheet')
sheet_name = 'bick 4'

import pandas as pd
df = pd.read_csv('/Users/macbook/Documents/Atom Projects/Sheets1/report.csv')#, index_col = '(Child) ASIN')
Luna = (df['(Child) ASIN'] == 'B0716JTGSX')
LU = df[Luna]

values = [LU.columns.values.tolist()]
values.extend(LU.values.tolist())
spreadsheet.values_update(sheet_name, params={'valueInputOption': 'USER_ENTERED'}, body={'values': values})

Pattern 2:模式二:

In this pattern, the value of df[Luna] is put to the Spreadsheet using the method of values_update .在此模式中,使用values_update方法将df[Luna]的值放入电子表格。

Sample script:示例脚本:

client = gspread.authorize(creds)
spreadsheet = client.open('Test Sheet')
sheet_name = 'bick 4'

import pandas as pd
df = pd.read_csv('/Users/macbook/Documents/Atom Projects/Sheets1/report.csv')#, index_col = '(Child) ASIN')
Luna = (df['(Child) ASIN'] == 'B0716JTGSX')
LU = df[Luna]

values = LU.values.tolist()
spreadsheet.values_append(sheet_name, params={'valueInputOption': 'USER_ENTERED'}, body={'values': values})

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 gspread 和谷歌表 API 更改谷歌表中的列格式 - Changing column format in google sheets by gspread and google sheets API 我想从谷歌表格中获取一个值的单行 - gspread - I want to fetch a single row for a value from google sheets - gspread 使用 gspread 和 gspread_dataframe 从 Google 表格导入数据时强制使用特定的单元格类型 - Force a specific cell type when importing data from Google sheets with gspread and gspread_dataframe 如何强制 google colab 和 gspread 以正确和各自的格式将字符串和日期写入 google 表格 - How to force google colab and gspread to write strings and dates to google sheets in the correct and respective format 如何使用 gspread 缓存 Google 表格的授权? - How to cache authorization for Google Sheets using gspread? 如何通过 gspread-formatting 在 Google DOCS API 中创建 DataValidationRule? - How can i create DataValidationRule in Google DOCS API via gspread-formatting? 如何格式化谷歌表格以便正确导出我的数据? - How can I format google sheets so I can export my data properly? 如何将数据导出到谷歌表格 - How can I export data to google sheets 有没有办法使用 Python 的 gspread API 自动调整 Google 表格中的列? - Is there a way to autofit a column in Google Sheets using gspread API for Python? 如何使用 gspread python 将 output 数据框放在 D2 而不是 Google 工作表中的 A1 - how to place output data-frame in D2 instead of A1 in google sheets with gspread python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM