Search code examples
pythongoogle-sheetsgoogle-sheets-apipygsheets

GSheet wrap strategy in pygsheet


In the Pygsheet reference doc here it shows a wrap_strategy property.

wrap_strategy
How to wrap text in this cell. Possible wrap strategies: ‘OVERFLOW_CELL’, ‘LEGACY_WRAP’, ‘CLIP’, ‘WRAP’. Reference: api docs

But in actual code if I were to cell.wrap_strategy = 'WRAP' I get an error TypeError: 'str' object is not callable

Actual code snippet:

for cell in wsheet.range("L3:L20").pop():
    cell.wrap_strategy('WRAP')

Solution

  • I believe your goal as follows.

    • You want to set the wrap strategy using pygsheets.

    Modification points:

    • When I saw the script of wrap_strategy of pygsheets, it seems that this is the Class Cell and in this case, I think that in the case of for cell in wsheet.range("L3:L20").pop():, you can use cell.wrap_strategy = 'WRAP'. (In this case, it sets to "L20".)

    From this, how about modifying your script as follows?

    Modified script:

    for cell in wsheet.range("L3:L20").pop():
        cell.wrap_strategy = "WRAP"
    

    or, as other direction, how about using get_values as follows?

    for cell in wsheet.get_values("L3", "L20", returnas="range")._data.pop():
        cell.wrap_strategy = "WRAP"
    

    Note:

    • If above modification was not the direct solution of your issue, can you provide your whole script without your personal information? By this, I would like to confirm it.

    Reference:

    Added:

    From your following replying,

    I was expecting it to wrap L3 to L20. But it seems only L20 is being read in this for loop. Would you know how to make it so?

    When I saw your script in your question, you wanted to use the last element of wsheet.range("L3:L20") because of pop(). So I followed to it. From your replying, when you want to set the wap strategy for "L3:L20" by modifying your script, how about the following sample script?

    Sample 1:

    for cell in wsheet.range("L3:L20"):
        cell[0].wrap_strategy = "WRAP"
    

    Sample 2:

    for cell in wsheet.get_values("L3", "L20", returnas="range")._data:
        cell[0].wrap_strategy = "WRAP"