-2

I'm writting dataframes to google spreadsheets with gspread_dataframe, gspread. I'm trying to assign a yellow background to cells depends on the values with pandas style. https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html Is there a way to save the assigned background color to the google spreadsheet too? Thanks

1 Answers1

0

With this one, you can use gspread_formatting

from gspread_formatting import *

bGYellow = cellFormat(backgroundColor=color(1, 1, 0))

format_cell_range(worksheet, 'A1:E1', bGYellow)

Please see gspread-formatting for more details

NightEye
  • 10,634
  • 2
  • 5
  • 24
  • thanks. is there a way to set the background color to be red if the absolute value of 1 or 2 columns are greater than certain values (eg, df.y.abs()>1,df.y.abs()>2)? thanks – user7264299 Dec 17 '20 at 21:22
  • you need to loop all the values and then apply an `if` condition for each item, containing your conditions above. Inside that loop should be this after writing those data. – NightEye Dec 17 '20 at 21:44
  • @user7264299, for the color red, you need to use `color(1, 0, 0)`. `color` accepts values 0 - 1 (including decimals in between as intermediary values) for `red`, `green` and `blue` for the 1st, 2nd and 3rd parameters respectively. – NightEye Dec 17 '20 at 21:48
  • @user7264299, If you have additional questions that are outside the scope of your original question, kindly put it in another post. If your original question was already solved, please push the accept button. Other people who have the same problem with you can also base your question as a question which can be solved. If you don't find the button, feel free to tell me. stackoverflow.com/help/accepted-answer – NightEye Dec 17 '20 at 21:57
  • Would you please give an example how to apply an if condition to one column to high-light the cells in this column that meet the condition? thanks – user7264299 Dec 17 '20 at 23:35
  • 1
    style can be saved to excel. is it possible to save to google spreasheet? – user7264299 Dec 18 '20 at 18:19
  • @user7264299, saving style to google spreadsheet should be possible. For your additional questions, please put it in another post as it is not related with your original question and other users as well will be able to check your issue. – NightEye Dec 18 '20 at 18:26
  • thanks. I tried to post a new question. however I was restricted to ask a new question. Maybe at most 1 question per day? do you have any idea how to save style to google spreadsheet? this is related to my original question. – user7264299 Dec 18 '20 at 18:39
  • @user7264299, see 4.1.15 in https://buildmedia.readthedocs.org/media/pdf/gspread/latest/gspread.pdf – NightEye Dec 18 '20 at 19:01
  • limits on new user accounts, please see here: https://meta.stackexchange.com/questions/164899/the-complete-rate-limiting-guide – NightEye Dec 18 '20 at 19:47