3

I'm trying to update some cell ranges with defined format, but I'm running into a problem when the ranges overlap. For example, if I have a range of cells A1:C3 and I want to set cells A2:B2 to have a green background color, and then to have them all have bold font style, the result is only the latter - the background color of the previous cells is overwritten by the bold style. If I do it in the reverse order, the cells have green background but no bold font. I'm using pygsheets library and this is roughly how my calls work:

# wks is a worksheet object from the created spreadsheet
wks.range("A2:B2", "range").apply_format(L_GREEN_BG, fields="userEnteredFormat")
wks.range("A1:C3", "range").apply_format(BOLD, fields="userEnteredFormat")

The BOLD and L_GREEN_BG are cell objects initialized with a dictionary in json-like format specified by Google Sheets API to match these updates.

My question is: is there any way to not overwrite previously made changes? Doing it sequentially like this would be a lot more handy than making more complex updates to singular cells.

Maciej B. Nowak
  • 1,180
  • 7
  • 19

2 Answers2

3

if you want other properties to be unchanged, be more specific on the fields. So in your first case set fields='userEnteredFormat\backgroundColor' and in your second request "userEnteredFormat\textFormat"

Nithin
  • 5,470
  • 37
  • 44
0

OK, upon reading the source code of the pygsheets library I think I know why this happens. The apply_format method sends a repeatCell request, which basically copies all of the provided cell's properties and applies them to the all of the cells in provided range. So when I provide a cell (the BOLD/L_GREEN_BG objects, which are just a dummy cell objects with only one property defined) with only bold font style, all of the cells in range get all of it's properties, along with default values. Every other subsequent call works similarly.

Maciej B. Nowak
  • 1,180
  • 7
  • 19