我有一个 blob 数据。我想使用 google sheet API v4 将其上传到 google sheet 中的单元格。
我看过这里的文档。 https://developers.google.com/sheets/api/guides/values
我也在这里查看了 SO 问题。 Insert image into Google Sheets cell using Google Sheets API
result = service.spreadsheets().values().update(
spreadsheetId=spreadsheet_id, range=range_name,
valueInputOption=value_input_option, body=body).execute()
我没有看到任何描述将 blob 作为图像插入的服务。请帮忙。
根据以下建议,我们从此处实现了 Webapp - Insert image into Google Sheets cell using Google Sheets API
这就是我们从 Python 代码中调用 Web 应用程序的方式
dropoff_signature = "ZGF0YT <clip > WVhSaA=="
web_app_url = "https://script.google.com/macros/s/A < clip > y/exec"
image_data = "data:image/png;base64," + dropoff_signature
data_to_post = {
'spreadsheetid' : spreadsheet_Id,
'sheetname' : 'Sheet1',
'imageurl' : image_data,
'column' : 5,
'row' : 5
}
encoded_data = urllib.urlencode(data_to_post)
# Send encoded data to application-2
url_result = urlfetch.fetch(web_app_url, encoded_data, method='POST')
我们在 Web 应用程序中看到以下错误。
result : 200 content : {"status":"error","defaultMessage":"Error retrieving image from URL or bad URL: data:image/png;base64, <clip> ","name":"Exception","fileName":"Code (Insert image into spreadsheet)","lineNumber":42,"stack":"\tat Code (Insert image into spreadsheet):42 (doPost)\n"}}
你能帮忙吗?
进行了此更改。仍然收到错误的 URL 错误。
dropoff_signature = "ZGF0YTpp<clip>WVhSaA=="
web_app_url = "https://script.google.com/macros/s/A<clip>y/exec"
image_data = "data:image/png;base64," + dropoff_signature
data_to_post = {
'spreadsheetid' : spreadsheet_Id,
'sheetname' : 'Sheet1',
'imageurl' : image_data,
'column' : 5,
'row' : 5
}
# encoded_data = urllib.urlencode(data_to_post)
# Send encoded data to application-2
# url_result = urlfetch.fetch(web_app_url, encoded_data, method='POST')
url_result = urlfetch.fetch(url=web_app_url, payload=json.dumps(data_to_post), method='POST', headers={'Content-type': 'application/json'})
result : 200 content : {"status":"error","defaultMessage":"Error retrieving
image from URL or bad URL:
data:image/png;base64,Z<clip>A==","error":
{"message":"Error retrieving image from URL or bad URL: data:image/png;base64,Z<clip>A==","name":"Exception","fileName":"Code (Insert image into spreadsheet)","lineNumber":42,"stack":"\tat Code (Insert image into spreadsheet):42 (doPost)\n"}}
这是我们正在使用的 Webapp。
function doGet(e) {
return ContentService.createTextOutput("Authorization: Bearer " +
ScriptApp.getOAuthToken())
}
//
// Example curl command to insert an image:
//
// curl -L -d '{ "spreadsheetid": "1xNDWJXOekpBBV2hPseQwCRR8Qs4LcLOcSLDadVqDA0E","sheetname": "Sheet1", "imageurl": "https://www.google.com/images/srpr/logo3w.png", "column": 1, "row": 1 }' \
// -H "Authorization: Bearer <INSERT TOKEN RETURNED FROM GET HERE>" \
// -H 'Content-Type: application/json' \
// https://script.google.com/a/tillerhq.com/macros/s/AKfycbzjFgIrgCfZTvOHImuX54G90VuAgmyfz2cmaKjrsNFrTzcLpNk0/exec
//
var REQUIRED_PARAMS = [
'spreadsheetid', // example: "1xNDWJXOekpBBV2hPseQwCRR8Qs4LcLOcSLDadVqDA0E"
'sheetname', // Case-sensitive; example: "Sheet1"
'imageurl', // Can be an url such as "https://www.google.com/images/srpr/logo3w.png"
// or alternately "data:image/png;base64,iVBOR...<snip>...gg=="
'column', // 1-based (i.e. top left corner is column 1)
'row' // 1-based (i.e. top left corner is row 1)
];
function doPost(e) {
var result = {
status: "ok",
defaultMessage: "Image inserted."
}
try {
var params = (e.postData && e.postData.type == "application/x-www-form-urlencoded") ? e.parameter
: (e.postData && e.postData.type == "application/json") ? JSON.parse(e.postData.contents)
: undefined;
if (!params) throw new Error('Unsupported content-type, must be either application/x-www-form-urlencoded or application/json.');
REQUIRED_PARAMS.forEach(function(requiredParam) {
if (!params[requiredParam]) throw new Error('Missing required parameter ' + requiredParam);
});
SpreadsheetApp.openById(params.spreadsheetid).getSheetByName(params.sheetname).insertImage(params.imageurl, params.column, params.row);
} catch(e) {
console.error(e);
result.status = "error";
result.error = e;
result.defaultMessage = e.message;
}
return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON)
}
最佳答案
解决方案一:
在您的 Python 应用程序中,您可以使用以下代码使用 Sheets API [1] 通过 IMAGE 公式设置图像。您需要输入您的电子表格 ID 并更改您想要图像的范围。
spreadsheet_id = '[SPREADSHEET-ID]'
range_name = 'D13'
service = build('sheets', 'v4', credentials=creds)
values = [
[
'=IMAGE("https://google.com","google")'
]
]
body = {
'values': values
}
result = service.spreadsheets().values().update(
spreadsheetId=spreadsheet_id, range=range_name,
valueInputOption='USER_ENTERED', body=body).execute()
解决方案 2:
如果您想使用 Apps 脚本中的 insertImage 函数 [2],将网格图像插入到表格中,而不是链接到单元格的图像。您可以部署一个带有 doPost() 函数的 Web 应用程序 [3],您可以在其中执行此操作,并使用服务帐户凭据从您的 Python 应用程序调用该 Web 应用程序。此外,您还需要部署 Web 应用程序以作为“访问 Web 应用程序的用户”执行,以便您从 Web 应用程序执行的所有请求都将使用服务帐户凭据进行。
Python 脚本:
from google.oauth2 import service_account
import requests
import json
import google.auth.transport.requests
SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = 'service_account.json'
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
delegated_credentials = credentials.with_subject('[USER-EMAIL-TO-IMPERSONATE]')
delegated_credentials.refresh(google.auth.transport.requests.Request())
token = delegated_credentials.token
headers = {'content-type': 'application/json', 'Authorization': 'Bearer ' + token}
url = '[WEB-APP-URL]'
data = {"file": '[blob]'}
response = requests.post(url, data=json.dumps(data), headers=headers)
网络应用程序脚本:
function doPost(e) {
var ss = SpreadsheetApp.openById('[SPREADSHEET-ID]');
var sheet = ss.getSheets()[0];
var blob = DriveApp.getFileById("[IMAGE-ID]").getBlob();
sheet.insertImage(blob, 4, 14);
return ContentService.createTextOutput("Good");
}
我使用从 Web 应用程序中的云端硬盘获取的图像测试了我的代码。您可以跳过该部分,直接从您的 Python 应用程序的数据负载中发送 blob。
要使用服务帐户,请记住授予 API 对所有所需范围的访问权限,您需要转到 admin.google.com->security->Setting->Advance Settings->Manage API client access 并使用服务帐户客户编号。
[1] https://developers.google.com/sheets/api/guides/values
[2] https://developers.google.com/apps-script/reference/spreadsheet/sheet#insertImage(BlobSource,Integer,Integer)
[3] https://developers.google.com/apps-script/guides/web
关于google-apps-script - 如何使用 google sheet api v 4 将 blob(图像)上传到 google sheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58176687/
相关文章:
c# - 程序集绑定(bind)重定向 : How and Why?
exception - Flutter:应用程序崩溃并出现运行时异常 - 回复已提交
reactjs - 错误 : [mobx] Computed values are not allo
python - 如何在没有数据库的情况下使用 JWTTokenUserAuthentication
selenium - Appium/WinAppDriver 无法找到上下文菜单 - 但仅在某些机器
android-studio - Android studio 在执行 lint 时找不到 kotl
google-chrome - 一些 Font Awesome 图标在 Chrome 中不显示
python - 使用 Python 类型模块指定序列或列表的长度