如何使用 python 替换 google 工作表中以开头的所有单元格
How to replace all cells that starts with in google sheets using python
我正在尝试替换以“条形码”开头的列中的所有值。但是现在,我可以根据值的名称替换所有值。
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('extended-signal-307001-
4e0bee527c0d.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open('test').sheet1
column_o = wks.findall('barcode recognition error', in_column=15)
print(column_o)
for cell in column_o:
cell.value = 'barcode error'
wks.update_cells(column_o)
目前,只有在“条形码识别错误”列中找到准确值时才会替换。我想找到以“条形码”开头的值,并将所有这些值替换为“条形码错误”。
从 findall
方法中删除 recognition error
,您应该已经准备就绪。
column_o = wks.findall('barcode')
我认为您的目标和现状如下。
- 您想将包含
barcode
的单元格值替换为 barcode error
。
- 您想通过 python.
使用 gspread 来实现此目的
- 您已经能够使用表格 API.API.
获取和输入 Google 电子表格的值
在这种情况下,我建议使用Sheets的batchUpdate方法API。我认为当使用 batchUpdate 方法时,您的目标可以通过一次 API 调用来实现。示例脚本如下
示例脚本:
请使用您的脚本来检索 credentials
。
gc = gspread.authorize(credentials)
spreadsheet = gc.open('test')
wks = spreadsheet.sheet1
body = {
"requests": [
{
"findReplace": {
"find": "^barcode[\s\w]+",
"searchByRegex": True,
"range": {
"sheetId": wks.id,
},
"replacement": "barcode error"
}
}
]
}
spreadsheet.batch_update(body)
- 在此示例脚本中,电子表格第一个选项卡中的所有单元格都被检查并替换为
barcode error
。如果要查看具体的列和行,请设置gridRange为range
.
参考文献:
我正在尝试替换以“条形码”开头的列中的所有值。但是现在,我可以根据值的名称替换所有值。
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('extended-signal-307001-
4e0bee527c0d.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open('test').sheet1
column_o = wks.findall('barcode recognition error', in_column=15)
print(column_o)
for cell in column_o:
cell.value = 'barcode error'
wks.update_cells(column_o)
目前,只有在“条形码识别错误”列中找到准确值时才会替换。我想找到以“条形码”开头的值,并将所有这些值替换为“条形码错误”。
从 findall
方法中删除 recognition error
,您应该已经准备就绪。
column_o = wks.findall('barcode')
我认为您的目标和现状如下。
- 您想将包含
barcode
的单元格值替换为barcode error
。 - 您想通过 python. 使用 gspread 来实现此目的
- 您已经能够使用表格 API.API. 获取和输入 Google 电子表格的值
在这种情况下,我建议使用Sheets的batchUpdate方法API。我认为当使用 batchUpdate 方法时,您的目标可以通过一次 API 调用来实现。示例脚本如下
示例脚本:
请使用您的脚本来检索 credentials
。
gc = gspread.authorize(credentials)
spreadsheet = gc.open('test')
wks = spreadsheet.sheet1
body = {
"requests": [
{
"findReplace": {
"find": "^barcode[\s\w]+",
"searchByRegex": True,
"range": {
"sheetId": wks.id,
},
"replacement": "barcode error"
}
}
]
}
spreadsheet.batch_update(body)
- 在此示例脚本中,电子表格第一个选项卡中的所有单元格都被检查并替换为
barcode error
。如果要查看具体的列和行,请设置gridRange为range
.