Search code examples
pythonpandasgoogle-sheetsgspread

Rounding a number in google sheets using gspread API


I am writing a pandas dataframe to google sheets using gspread:

from gspread_formatting import *
import gspread
from df2gspread import df2gspread as d2g
import pandas as pd

d2g.upload(data, sheet.id, 'test_name', clean=True, credentials=creds, col_names=True, row_names=False)

While the pandas dataframe is rounded to 2 decimal points the value in google sheets sometimes have more than that.

df

a               b
100.56          600.79

Result in google sheets:

aa                b
100.5616          600.79

And I can't find any information how I can round a value using python gspread API.


Solution

  • If you get the worksheet element you can use format to achieve what you want.

    sh = gc.open("sheet_name")
    worksheet = sh.get_worksheet(0) # your sheet number
    worksheet.format('A', {'numberFormat': {'type' : 'NUMBER', 'pattern': '0.0#'}})