电子表格 API - 当电子表格在某些列中有公式时附加值
Spreadsheets API - appending values when the spreadsheet has formulas in some columns
我正在使用 spreadsheets api 将值附加到 sheet 但是这个 sheet 有一个特殊性,它有一些包含处理公式的列我应该在其他列中引入的数据,所以我不能删除这些列,但是当我尝试附加一个值时,该值附加在末尾,公式未定义,e.i带有公式的列使我的代码跳过行,直到没有公式,这是我用来附加
的一段代码
import os
import pickle
import os.path
import io
import shutil
from lector_nombre import lectorNombre
from Tablas import lectorTablas
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseDownload
SCOPES = ['https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets']
#SAMPLE_SPREADSHEET_ID = '1BxiMVs0ew23gmUUqptlb23frvfvE2upms'
#SAMPLE_RANGE_NAME = 'Class Data!A2:E'
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
data=[["data","data","data","data","https://google.com"]]
res = sheet.values().append(spreadsheetId="134542fvbvfdfewdvwed4",
range="sheet!A1:G1",
valueInputOption="USER_ENTERED",
insertDataOption="INSERT_ROWS",
body={"values":data}).execute()
print(res)
这里有一张我的 sheet 长相的图片
编辑:
我想我的问题不是很清楚,我真正需要的是指定一个范围,让我写在我想写的地方,而不会因为公式的原因而跳过行
你是appending values using spreadsheets.values.append你基本上是在添加一个新行并设置它的数据。
您需要使用 spreadsheets.values.update,它可以让您在不添加新行或列的情况下修改电子表格范围内的值。
示例:
输出:
我正在使用 spreadsheets api 将值附加到 sheet 但是这个 sheet 有一个特殊性,它有一些包含处理公式的列我应该在其他列中引入的数据,所以我不能删除这些列,但是当我尝试附加一个值时,该值附加在末尾,公式未定义,e.i带有公式的列使我的代码跳过行,直到没有公式,这是我用来附加
的一段代码import os
import pickle
import os.path
import io
import shutil
from lector_nombre import lectorNombre
from Tablas import lectorTablas
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseDownload
SCOPES = ['https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets']
#SAMPLE_SPREADSHEET_ID = '1BxiMVs0ew23gmUUqptlb23frvfvE2upms'
#SAMPLE_RANGE_NAME = 'Class Data!A2:E'
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
data=[["data","data","data","data","https://google.com"]]
res = sheet.values().append(spreadsheetId="134542fvbvfdfewdvwed4",
range="sheet!A1:G1",
valueInputOption="USER_ENTERED",
insertDataOption="INSERT_ROWS",
body={"values":data}).execute()
print(res)
这里有一张我的 sheet 长相的图片
编辑:
我想我的问题不是很清楚,我真正需要的是指定一个范围,让我写在我想写的地方,而不会因为公式的原因而跳过行
你是appending values using spreadsheets.values.append你基本上是在添加一个新行并设置它的数据。
您需要使用 spreadsheets.values.update,它可以让您在不添加新行或列的情况下修改电子表格范围内的值。
示例:
输出: