python-3.x - 使用公式化字符写入 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']
解决方案
怎么用parse=False
?
当我看到脚本时,默认是parse=True
. 在这种情况下,valueInputOption
使用USER_ENTERED
. 使用时parse=False
,valueInputOption
使用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"
参考:
推荐阅读
- powerbi - SUMMARIZECOLUMNS 中的动态列选择
- c++ - 当我的代码运行良好时,为什么在 SPOJ 中出现运行时(SIGABRT)错误?
- mysql - 用于更新mysql中每个重复记录的自动增量值的存储过程
- javascript - 抽屉未在本机中打开
- angular - 如何获取子组件?
- vue.js - 在引导 vue 表中使用图标
- amazon-web-services - 捕获 cloudformation 更新命令的输出响应代码
- postgresql - 如何使用约束和索引交换 postgres 数据库中的两个表?
- bash - 如何在 shell 脚本中优雅地处理内存不足
- java - 在 java 中解析 XML 时没有工作 ID 属性