내 프로젝트는 Ruby on Rails에 의해 작성되었습니다. google-api-client gem은 시트 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 앱 스크립트가 이미지 데이터를 추출 할 수 없습니다. 그러나 문제가 발생하면 오류 로그의 이미지 URL 위를 브라우저에 수동으로 복사하여 붙여 넣으면 이미지를 성공적으로 다운로드 할 수 있습니다. 이미 이미지 URL에 대해 공개를 설정했습니다.
아래는 내 Google 앱 스크립트 부분입니다.
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
누구든지 이것에 대한 경험이 있습니까?
이것은 내 이해입니다.
Server error
발생합니다.이 경우에도 때 setWidth
와 setHeight
사용되는 오류를 제거 할 수 없다. 이미지를 삽입 할 때 오류가 발생하기 때문입니다. 그래서이 답변에서는 Google Apps Script 라이브러리에서 크기를 조정하여 이미지를 삽입하는 것을 제안하고 싶습니다.
스크립트를 수정하면 다음과 같이됩니다. 이 스크립트를 사용하기 전에 ImgApp의 GAS 라이브러리 를 설치하십시오 .
function addImageOnSpreadsheet(spreadsheet_id, sheet_name, image_file_id, column, row, width, height) {
let blobSource = DriveApp.getFileById(image_file_id).getBlob();
const obj = ImgApp.getSize(blobSource);
if (obj.height * obj.width > 1048576) {
blobSource = ImgApp.doResize(image_file_id, width).blob;
}
let image = SpreadsheetApp.openById(spreadsheet_id).insertImage(blobSource, column, row);
image.setWidth(width).setHeight(height);
}
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/script.external_request
https://www.googleapis.com/auth/spreadsheets
이 샘플 스크립트는 여러 파일 ID를 받기위한 것입니다.
function addImageOnSpreadsheet(ar) {
ar.forEach(o => {
let blobSource = DriveApp.getFileById(o.image_file_id).getBlob();
const obj = ImgApp.getSize(blobSource);
if (obj.height * obj.width > 1048576) {
blobSource = ImgApp.doResize(o.image_file_id, o.width).blob;
}
var image = SpreadsheetApp.openById(o.spreadsheet_id).insertImage(blobSource, o.column, o.row);
image.setWidth(o.width).setHeight(o.height);
});
}
예를 들어 ar
다음과 같습니다.
ar = [
{spreadsheet_id: "###", image_file_id: "###", column: ##, row: ##, width: ###, height: ###},
{spreadsheet_id: "###", image_file_id: "###", column: ##, row: ##, width: ###, height: ###},
,
,
];
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다