When I use gspread to copy a cell formula from (target_cell) to (target_cell+1 row) the formula is correctly passed, however it is still referencing data from (target_cell)'s row. I need it to reference the new row's data. Similar to when you use the "drag" or copy+shift+select all appropriate cells+paste method in excel/google sheets.
I currently can use gspread to get a formula from a particular cell using this: ([source] How to copy a formula from one gsheet to another using python?)
formula = sheet.acell("C3", value_render_option='FORMULA').value
print(formula)
I want to copy this formula (=B3+A3) in the row above but reference A2 and B2 isntead. The only way I can see so far to update a cell is like this, however can I manipulate the formula in between?
sheet.update_acell("C2", formula)
Here is the entire code using the first response I got:
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)