如何将单元格公式复制到上面的 1 行,但引用新行的数据(而不是前几行的数据)

How to copy a cell formula into 1 row above, but reference the new row's data (and not the previous rows data)

当我使用 gspread 将单元格公式从 (target_cell) 复制到 (target_cell+1 行) 时,公式已正确传递,但它仍然引用来自 (target_cell) 的行。我需要它来引用新行的数据。类似于在 excel/google 工作表中使用 "drag" 或复制+shift+select 所有适当的单元格+粘贴方法。

我目前可以使用 gspread 从特定单元格获取公式:([source] )

formula = sheet.acell("C3", value_render_option='FORMULA').value
    print(formula)

我想在上面的行中复制这个公式 (=B3+A3),但引用 A2 和 B2 是无效的。到目前为止,我能看到的更新单元格的唯一方法是这样的,但是我可以在两者之间操纵公式吗?

sheet.update_acell("C2", formula)

这是使用我得到的第一个响应的完整代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

#this portion is to ensure I can reach the target spreadsheet and manipulate the spreadsheet. It does work.
sheet = client.open("stocks").worksheet("sandbox")
input=sheet.find(r"start")
tgtrow=input.row
tgtcol=(input.col)
sheet.insert_row(["",""],tgtrow+1)
sheet.update_cell(tgtrow,tgtcol+4, "hi")

#running recomended code
spreadsheetId = "stocks"
sheetName = "sandbox"

client = gspread.authorize(creds)
ss = client.open_by_key(spreadsheetId)
sheetId = ss.worksheet(sheetName)._properties['sheetId']
body = {
    "requests": [
        {
            "copyPaste": {
                "source": {
                    "sheetId": sheetId,
                    "startRowIndex": 2,
                    "endRowIndex": 3,
                    "startColumnIndex": 2,
                    "endColumnIndex": 3
                },
                "destination": {
                    "sheetId": sheetId,
                    "startRowIndex": 1,
                    "endRowIndex": 2,
                    "startColumnIndex": 2,
                    "endColumnIndex": 3
                },
                "pasteType": "PASTE_FORMULA"
            }
        }
    ]
}
res = ss.batch_update(body)
  • 您想使用 gspread 将公式复制到单元格。
  • 比如"C3"单元格中有=B3+A3的公式,想把公式=B2+A2放到"C2"单元格中。
  • 您已经使用过带有 gspread 的表格 API。

如果我的理解是正确的,这个修改怎么样?我认为您的情况有几种解决方案。所以请把这当作其中之一。

修改点:

  • 本次修改,copyPaste请求被Sheets中的batchUpdate方法使用API。

修改后的脚本:

使用时请设置spreadsheetIdsheetName的变量。在此示例脚本中,对于 "Sheet1" 的 sheet 名称,单元格 "C3" 的公式被复制到单元格 "C2"。届时=B3+A3自动修改为=B2+A2.

spreadsheetId = "###"
sheetName = "Sheet1"

client = gspread.authorize(credentials)
ss = client.open_by_key(spreadsheetId)
sheetId = ss.worksheet(sheetName)._properties['sheetId']
body = {
    "requests": [
        {
            "copyPaste": {
                "source": {
                    "sheetId": sheetId,
                    "startRowIndex": 2,
                    "endRowIndex": 3,
                    "startColumnIndex": 2,
                    "endColumnIndex": 3
                },
                "destination": {
                    "sheetId": sheetId,
                    "startRowIndex": 1,
                    "endRowIndex": 2,
                    "startColumnIndex": 2,
                    "endColumnIndex": 3
                },
                "pasteType": "PASTE_FORMULA"
            }
        }
    ]
}
res = ss.batch_update(body)

参考文献:

如果我误解了您的问题而这不是您想要的结果,我深表歉意。

编辑:

问题:

从你的附加脚本中,我可以理解这个问题。问题是您使用 Spreadsheet 的文件名(在您的例子中,它是 stocks.)作为 Spreadsheet ID。如果要使用Spreadsheet的文件名,请修改如下

发件人:

spreadsheetId = "stocks"

收件人:

spreadsheetId = client.open("stocks").id