如何使用python将公式从一个gsheet复制到另一个gsheet? [英] How to copy a formula from one gsheet to another using python?
问题描述
我不确定这是否可行.我有大量的电子表格,需要更新公式.如何将公式从一个单元格或一组单元格复制到另一个单元格?我用过gspread
,它似乎只能做值.我需要python才能基本上将公式粘贴到几百张纸上,而无需我单独打开每个公式并复制并粘贴公式.
I'm not sure if this is possible. I have tons of spreadsheet, and the formulas need to be updated. How do I copy a formula from one cell or a group of cells to another? I've used gspread
and it seems it can only do values. I need python to basically paste formulas on hundreds of sheets for me, without me opening each individually and copy and pasting the formulas.
有人对复制和粘贴公式有通用的解决方案吗?这非常重要,您会认为有人可以做到.
Does anybody have a generic solution for copying and pasting formulas? This is pretty important, you would think someone can do it.
推荐答案
2018年7月19日更新:
这是您的操作方式:
Update 19 July 2018:
Here's how you do it:
# Get the formula value from the souce cell:
formula = wks.acell('A2', value_render_option='FORMULA').value
# Update the target cell with formula:
wks.update_acell('B3', formula)
(从使用Sheets API v4的gspread 3.0.0开始有效.)
(this works since gspread 3.0.0 which uses Sheets API v4.)
要访问公式值,需要使用单元格对象的input_value
属性.
To access a formula value, you need to use the input_value
attribute of a cell object.
这是一个例子. (我跳过了初始化步骤.假设您已经打开了电子表格,并且wks
是指您
Here's an example. (I'm skipping the initialization step. Let's assume you've already opened a spreadsheet and wks
is referring to you worksheet object.)
# To copy a formula from a single cell (say, A2) to another cell (B3)
# use the input_value property
formula = wks.acell('A2').input_value
# then paste the value to a new cell
wks.update_acell('B3', formula)
对于一组单元格,您想使用wks.range()
方法来获取单元格对象.然后,可以像上面的代码一样通过input_value
获取公式值.参见GitHub上的示例.
For a group of cells, you'd want to use a wks.range()
method to get cell objects. Then you can get formula values via input_value
as in the code above. See the example on the GitHub.
这篇关于如何使用python将公式从一个gsheet复制到另一个gsheet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!