首页 > 解决方案 > 使用公式化字符写入 gsheets 的 pygsheets 数据将数据作为公式

问题描述

使用 pygsheets 将数据写入 gsheets 时 - 我的一个值包含一个+字符。例如+myvalue

然后导出数据时,我得到#NAME?输出,而不是背景值。#姓名?,当然公式栏包含正确的值。+我的价值

然而,这并不完全出乎意料 - 当使用 sheet 方法并手动导入包含这些值的 CSV 时 -#NAME?不会显示错误,而是我可以在字段中看到 +myvalue。(除非我编辑它。)

这是我“导入” csv 的代码 - 当然它只是读取 csv 并加载值:

# Authorise with GSheets Service Account
gc = pygsheets.authorize(service_file=service_account_file)

# Open spreadsheet
sh = gc.open_by_key(spreadsheet_key)

# Open Worksheet
#wks = sh.add_worksheet(spreadsheet_hosts_worksheet) # Create Worksheet
wks = sh.worksheet_by_title(worksheet_name)

# Generate list "data" with Values from CSV
with open(inputFile, 'r') as f:
  #reader = csv.reader(f, skipinitialspace=True, delimiter=',', quotechar='"')
  reader = csv.reader(f, delimiter=',', quotechar='"')
  data = list(reader)

# Empty Worksheet
wks.clear()

# Append Values
wks.update_values(crange='A1', values=data)

# Freeze Top Row
wks.frozen_rows=1

我可以更改更新方法,以便它采用文本等公式 - 与CSV import函数相同的方式GSheets吗?

我的样本数据:

['host_name', 'alias', 'address', 'parents', 'use', 'display_name', 'hostgroups', 'contacts', '_ADDINFO', '_SNOWGROUP', '_RTTCRIT', '_RTTWARN', 'contact_groups', 'notes', 'notes_url', 'check_command', 'first_notification_delay', 'check_interval', 'max_check_attempts', 'retry_interval', 'config_filename']
['host', 'destiny islands', '1.1.1.1', 'host.mypalace.com,host.mapalace2.com', 'tmpl_network_device', 'Gingerbread lane', 'hgrp_grandad', '+myvalue', 'ACTION - Don't forget to smile', 'test', '70', '20', '', '', '', '', '', '', '', '', '/folder/filename']

标签: python-3.xgoogle-sheetsgoogle-sheets-apipygsheets

解决方案


怎么用parse=False

当我看到脚本时,默认是parse=True. 在这种情况下,valueInputOption使用USER_ENTERED. 使用时parse=FalsevalueInputOption使用RAW. 由此,+myvalue#NAME?

修改后的脚本:

wks.update_values(crange='A1', values=[['+myvalue']])  # ---> #NAME? is put in a cell "A1"

wks.update_values(crange='A2', values=[['+myvalue']], parse=False)  # ---> +myvalue is put in a cell "A2"

参考:


推荐阅读