Google表格附加流是:
function insertLogoIntoDocument(body, userProperties) {
const functionName = "insertLogoIntoDocument";
const functionNameError = `${functionName}_error`;
basicLog(functionName, "LOGO_DEBUG_INIT", "Starting function");
try {
// Check if body is valid
if (!body) {
basicLog(functionNameError, "LOGO_DEBUG_INVALID_BODY", "Document body is null or undefined");
return { isError: true, message: "Invalid document body provided" };
}
const useCustomLogo = isUsingCustomLogo();
const logoTextElement = body.findText("<LOGO>");
if (useCustomLogo) {
try {
const logoImageId = userProperties.getProperty(LOGO_IMAGE_ID);
basicLog(functionName, "LOGO_DEBUG_CUSTOM_DETECTED", "Checking for logo placeholder");
// Log whether the logo placeholder was found
basicLog(functionName, "LOGO_DEBUG_SEARCH_RESULT", "Logo placeholder search completed");
if (logoTextElement) {
try {
const logoImage = DriveApp.getFileById(logoImageId);
const logoBlob = logoImage.getBlob();
const logoElement = logoTextElement.getElement();
const logoPosition = logoElement.getParent().getChildIndex(logoElement);
const logoParagraph = logoElement.getParent();
basicLog(functionName, "LOGO_DEBUG_CUSTOM_FILE_DATA",
`Logo ID: ${logoImageId}, File: ${logoImage.getName()}, Blob: ${logoBlob.getBytes().length} bytes, Element: ${logoElement.getType()}, Parent: ${logoParagraph.getType()}, Position: ${logoPosition}`
);
logoElement.setText("");
const inlineImage = logoParagraph.insertInlineImage(logoPosition, logoBlob);
const originalWidth = inlineImage.getWidth();
const originalHeight = inlineImage.getHeight();
const aspectRatio = originalWidth / originalHeight;
let targetHeight = 72;
let targetWidth;
try {
const savedLogoSize = userProperties.getProperty(LOGO_SIZE_PROPERTY);
basicLog(functionName, "LOGO_DEBUG_SIZE_RAW", "Processing saved logo size");
if (savedLogoSize) {
targetHeight = parseInt(savedLogoSize, 10);
if (isNaN(targetHeight)) {
basicLog(functionName, "LOGO_DEBUG_SIZE_INVALID", "Using default size");
targetHeight = 72;
}
} else {
basicLog(functionName, "LOGO_DEBUG_SIZE_MISSING", "Using default size");
targetHeight = 72;
}
basicLog(functionName, "LOGO_DEBUG_SIZE_FINAL", `Height: ${targetHeight}`);
} catch (err) {
const errorMessage = err.message || "No error message available";
const stackTrace = err.stack || "No stack trace available";
basicLog(functionNameError, "LOGO_DEBUG_SIZE_ERROR", `Error: ${errorMessage}, Stack: ${stackTrace}`);
// Continue with default height if there's an error
targetHeight = 72;
}
targetWidth = Math.round(targetHeight * aspectRatio);
inlineImage.setHeight(targetHeight);
inlineImage.setWidth(targetWidth);
basicLog(functionName, "LOGO_DEBUG_INSERT_SUCCESS", `Height: ${targetHeight}, Width: ${targetWidth}`);
return { isError: false, message: "Logo inserted successfully" };
} catch (err) {
const errorMessage = err.message || "No error message available";
const stackTrace = err.stack || "No stack trace available";
basicLog(functionNameError, "LOGO_DEBUG_INSERTION_ERROR", `Error: ${errorMessage}, Stack: ${stackTrace}`);
return { isError: true, message: "Error inserting logo: " + errorMessage };
}
} else {
try {
const firstParagraph = body.getParagraphs()[0] ? body.getParagraphs()[0].getText() : "No paragraphs";
const secondParagraph = body.getParagraphs()[1] ? body.getParagraphs()[1].getText() : "No second paragraph";
basicLog(functionName, "LOGO_DEBUG_PLACEHOLDER_MISSING", `First paragraph: ${firstParagraph}, Second paragraph: ${secondParagraph}`);
return { isError: true, message: "Logo placeholder not found in document" };
} catch (err) {
const errorMessage = err.message || "No error message available";
const stackTrace = err.stack || "No stack trace available";
basicLog(functionNameError, "LOGO_DEBUG_TEMPLATE_ERROR", `Error: ${errorMessage}, Stack: ${stackTrace}`);
return { isError: true, message: "Error checking document paragraphs" };
}
}
} catch (err) {
const errorMessage = err.message || "No error message available";
const stackTrace = err.stack || "No stack trace available";
basicLog(functionNameError, "LOGO_DEBUG_HANDLING_ERROR", `Error: ${errorMessage}, Stack: ${stackTrace}`);
return { isError: true, message: "Error handling logo: " + errorMessage };
}
} else if (logoTextElement) {
try {
body.replaceText("<LOGO>", "");
basicLog(functionName, "LOGO_DEBUG_PLACEHOLDER_REMOVED", "No custom logo used");
return { isError: false, message: "Logo placeholder removed successfully" };
} catch (err) {
const errorMessage = err.message || "No error message available";
const stackTrace = err.stack || "No stack trace available";
basicLog(functionNameError, "LOGO_DEBUG_REMOVAL_ERROR", `Error: ${errorMessage}, Stack: ${stackTrace}`);
return { isError: true, message: "Error removing logo placeholder" };
}
}
// Default return if no conditions above were met
return { isError: false, message: "No logo processing needed" };
} catch (err) {
const errorMessage = err.message || "No error message available";
const stackTrace = err.stack || "No stack trace available";
basicLog(functionNameError, "LOGO_DEBUG_UNEXPECTED_ERROR", `Error: ${errorMessage}, Stack: ${stackTrace}`);
return { isError: true, message: "Unexpected error in logo processing" };
}
}
我尝试了插入图像的不同变体,但是仍然“不允许动作”。 我还进行了测试,以确保没有同时登录多个Google帐户,并且仅确保1个帐户。
LOGO_DEBUG_CUSTOM_FILE_DATA. Additional Info: Logo ID: "123456...", File: image_name.png, Blob: 158768 bytes, Element: TEXT, Parent: PARAGRAPH, Position: 0.
LOGO_DEBUG_INSERTION_ERROR. Additional Info:
Error: Action not allowed, Stack: Exception: Action not allowed
at insertLogoIntoDocument (reportsGenerate/utils/logoHelper:38:47)
在显示的情况下,我假设附加组件能够成功检索图像。
同样,当我使用“测试部署”运行它时,它可以正常运行,它只是发布错误的版本。
这对我来说很好。用19kb测试256 x 256 PNG图像。Blob: 158768 bytes