我正在尝试使用python中的pygsheets旋转Google表格中某些长标题名称的文本。以下代码显示了对pygsheets中提供的示例的略微修改,根据docs应该可以使用。文本被写入正确的单元格,单元格A1为粗体,但单元格B1和C1没有旋转。没有错误消息浮出水面。知道为什么这行不通吗?
import pygsheets
sheet_url = "https://docs.google.com/spreadsheets/d/1t3nYyvkVkf6E36vTRucRQ35XQWbeRC--6U5h1chtikI"
gc = pygsheets.authorize(service_file = "credentials.json")
sh = gc.open_by_url(sheet_url)
wks = sh.worksheet("title", "Sheet1")
header = wks.cell('A1')
header.value = 'Bold Text'
header.set_text_format('bold', True)
header.update()
header2 = wks.cell('B1')
header2.value = '45 Degree rotated text'
header2.set_text_rotation('angle', 45)
header2.update()
header3 = wks.cell('C1')
header3.value = 'Vertical Text'
header3.set_text_rotation('vertical', True)
header3.update()
答案 0 :(得分:1)
如果我的理解正确,那么这个答案如何?在我的环境中,我还可以确认与您发生了相同的问题。因此,当我看到“ pygsheets”脚本时,我注意到了该脚本的修改点。
尽管设置了textRotation
的参数,但不幸的是,textRotation
未包含在请求正文中。因此,textRotation
不起作用。
使用此修改时,请按如下所示修改已安装“ pygsheets”目录中get_json()
中的cell.py
。我认为可能会有更简单的修改。因此,请考虑这只是几个答案之一。
def get_json(self):
"""Returns the cell as a dictionary structured like the Google Sheets API v4."""
try:
nformat, pattern = self.format
except TypeError:
nformat, pattern = self.format, ""
if self._formula != '':
value = self._formula
value_key = 'formulaValue'
elif is_number(self._value):
value = self._value
value_key = 'numberValue'
elif type(self._value) is bool:
value = self._value
value_key = 'boolValue'
elif type(self._value) is str or type(self._value) is unicode:
value = self._value
value_key = 'stringValue'
else: # @TODO errorValue key not handled
value = self._value
value_key = 'errorValue'
ret_json = dict()
ret_json["userEnteredFormat"] = dict()
if self.format[0] is not None:
ret_json["userEnteredFormat"]["numberFormat"] = {"type": getattr(nformat, 'value', nformat),
"pattern": pattern}
if self._color[0] is not None:
ret_json["userEnteredFormat"]["backgroundColor"] = {"red": self._color[0], "green": self._color[1],
"blue": self._color[2], "alpha": self._color[3]}
if self.text_format is not None:
ret_json["userEnteredFormat"]["textFormat"] = self.text_format
fg = ret_json["userEnteredFormat"]["textFormat"].get('foregroundColor', None)
if fg:
ret_json["userEnteredFormat"]["textFormat"]['foregroundColor'] = {"red": fg[0], "green": fg[1],
"blue": fg[2], "alpha": fg[3]}
if self.borders is not None:
ret_json["userEnteredFormat"]["borders"] = self.borders
if self._horizontal_alignment is not None:
ret_json["userEnteredFormat"]["horizontalAlignment"] = self._horizontal_alignment.value
if self._vertical_alignment is not None:
ret_json["userEnteredFormat"]["verticalAlignment"] = self._vertical_alignment.value
if self._wrap_strategy is not None:
ret_json["userEnteredFormat"]["wrapStrategy"] = self._wrap_strategy
### Added -- begin
if self.text_rotation is not None:
ret_json["userEnteredFormat"]["textRotation"] = self.text_rotation
### Added -- end
if self._note is not None:
ret_json["note"] = self._note
ret_json["userEnteredValue"] = {value_key: value}
return ret_json
反映出上述修改后,您的脚本将获得以下结果。
在我的环境中,我可以通过上述修改工作的“ textRotation”来确认。但是,如果这在您的环境中不起作用,我深表歉意。