【发布时间】:2020-04-06 06:30:47
【问题描述】:
我的项目是由 Ruby on Rails 编写的,因为 google-api-client gem 不支持通过 sheet api 将图像插入电子表格,所以我使用 Google 应用脚本来处理这个任务。大多数情况下,此调用的插入已成功:
@app_script.run_script(script_id, request)
我只是为好奇的人提供我的脚本: https://script.google.com/a/vectorgroup.vn/d/1ndcgpfJMj3YdKj0pEvHWz0pF4NtcQyR1Qg8wj7ZnpKfIwP2UsH0xaYq4/edit?splash=yes
问题是有时,调用失败并出现以下错误:
错误详情:[{\"@type\"=>\"type.googleapis.com/google.apps.script.v1.ExecutionError\", \"scriptStackTraceElements\"=>[{\"function\" =>\"addImageOnSpreadsheet\"}], \"errorMessage\"=>\"异常:从 URL 或错误 URL 检索图像时出错:https://drive.google.com/uc?id=1MS6KMfua7kZCSGMUhny4kDUvalxTkoKJ&export=download\", \"errorType\"=>\"ScriptError\"}] " 2020-04-06T06:03:33.821Z pid=26645 tid=tz8bh class=ImageTakerWorker jid=d847bf91beea8aeb4a30b042 elapsed=11.689 信息:完成 错误:3" "错误详情:[{\"@type\"=>\"type.googleapis.com/google.apps.script.v1.ExecutionError\", \"scriptStackTraceElements\"=>[{\"function\"=> \"addImageOnSpreadsheet\"}], \"errorMessage\"=>\"异常:从 URL 或错误 URL 检索图像时出错:https://drive.google.com/uc?id=1G9EDgvygwVztMG66FArZ6BEFpzW71izA&export=download\", \"errorType\"=>\"ScriptError\"}]" 2020-04-06T08:00:28.330Z pid=26645 tid=tz7zl class=ImageTakerWorker jid=a6b4fcb47db15f71dbf1d3f5 elapsed=6.514 信息:完成 “插入图像错误:#,#” “错误:3” "错误详情:[{\"@type\"=>\"type.googleapis.com/google.apps.script.v1.ExecutionError\", \"scriptStackTraceElements\"=>[{\"function\"=> \"addImageOnSpreadsheet\"}], \"errorMessage\"=>\"异常:从 URL 或错误 URL 检索图像时出错:https://drive.google.com/uc?id=1_3KzBDDgpINMCNkEZj2LivqdUaxFKZNT&export=download\", \"errorType\"=>\"ScriptError\"}]" 2020-04-06T08:00:28.941Z pid=26645 tid=tzahx class=ImageTakerWorker jid=8010f8c64cb9a0efa672b713 elapsed=7.121 信息:完成
问题似乎发生在 url 图片上,不知何故 google app 脚本无法提取图片数据。但是当出现问题时,我只需将错误日志中的上述图像网址手动复制并粘贴到浏览器,它就可以成功下载图像。 请注意,我已经为图片 url 设置了 public。
以下是我的谷歌应用脚本部分:
class GoogleAppScript
APPLICATION_NAME = "Insert image to spreadsheet".freeze
def initialize(user_id, sheet_id, options = {})
@user = User.find(user_id)
@sheet_id = sheet_id
@from_class = options[:from_class]
@service = options[:service]
@keyword = options[:keyword]
@sheet_name = options[:service] == "google" ? "G" + options[:keyword].strip : "Y" + options[:keyword].strip
@image_file_id = options[:image_file_id]
@google_authorization = GoogleAuthorization.new(@user).authorize
@app_script = Google::Apis::ScriptV1::ScriptService.new
@app_script.client_options.application_name = APPLICATION_NAME
@app_script.authorization = @google_authorization
end
def execute
script_id = "1ndcgpfJMj3YdKj0pEvHWz0pF4NtcQyR1Qg8wj7ZnpKfIwP2UsH0xaYq4"
url_image = "https://drive.google.com/uc?id=#{@image_file_id}&export=download"
start_col = 1
if @from_class == "ImageX"
start_row = 51
width = 692
height = 1500
else # "ImageY"
start_row = @keyword.last == " " ? 30 : 9
width = 694
height = 418
end
request = Google::Apis::ScriptV1::ExecutionRequest.new(
function: "addImageOnSpreadsheet",
parameters: [@sheet_id, @sheet_name, url_image, start_col, start_row, width, height],
)
begin
response = @app_script.run_script(script_id, request)
if response.error
# Retry until success
# ImageTakerWorker.perform_async(@user.id, @sheet_id, @sheet_name, @image_file_id)
p "Insert image ERROR: #{response}, #{response.error}"
p "Error: #{response.error.code}"
p "Error detail: #{response.error.details}"
else
p "Insert image successfully"
end
rescue Google::Apis::ClientError
# Handle the error...
end
end
end
有人有这方面的经验吗?
【问题讨论】:
-
您能否提供示例 URL 以复制您的问题?或者,如果您在脚本编辑器中直接使用 Google Apps Script 中的 URL 时没有出现错误,您能否提供请求正文或您的脚本?
-
@Tanaike 我刚刚将它们附加到问题中。
-
感谢您的回复和补充信息。首先,从您的回复来看,当您直接在 Google Apps 脚本中使用该 URL 时,不会发生错误。我的理解正确吗?而且,对于 webContentLink,文件需要公开共享。你能确认吗?并且,您能确认该文件是否是可以放入的图像文件吗?此外,当您将文件 ID 作为参数并通过在脚本中检索来使用图像 blob 时,您会得到什么结果?如果这些没有导致解决方案或问题的原因,我很抱歉。
-
感谢您的建议,我的意思是当脚本执行错误时,如果我将图像URL手动复制并粘贴到Web浏览器,我仍然可以看到浏览器可以很好地下载图像。正如我所说,我已经为我的图像文件夹设置了网络上的公共权限。这意味着所有图像都是公开的。然后我还尝试修改我的应用程序脚本和参数以通过 webContentLink 检索图像 url,我观察到相同的错误日志。可以在这里找到:script.google.com/d/…。
-
我尝试运行我的应用程序几次,有时它可以完成插入而没有任何错误。有时,会发生上述错误。
标签: ruby-on-rails google-apps-script google-sheets google-apps-script-api