Reputation: 303
So the basic idea is to have a picture in Google sheet and have Text on top of it that would change based on variables in the sheet
So my question is:
Is there a way to do something like this using either cells somehow showing on top of the picture, Or have a drawingwith a text box that you can use logic with so that it can change the value accordingly?
Upvotes: 0
Views: 920
Reputation: 201428
Unfortunately, I think that your goal cannot be directly achieved using the existing methods of Spreadsheet services. So, as a workaround, in your situation, how about using this method? In this method, 2 Google Apps Script libraries are used. Using these libraries, the following flow is run.
By this flow, I thought that your goal might be able to be achieved.
Please install 2 Google Apps Script libraries as follows.
Please enable Drive API at Advanced Google services.
Before you use this script, please set the fileId, sheet name, and cell range. In this script, the image on Google Drive is used. Because when the image on Spreadsheet is retrieved, the process cost becomes high. So I used this method.
function myFunction() {
const fileId = "###"; // Please set the file ID of image file.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set the sheet name.
const value = sheet.getRange("A1").getDisplayValue(); // Please set the range.
// Please set the text.
const text = { text: value, left: 10, top: 120, width: 180, height: 60, fontSize: 20 }; // You can manage the inserted text here.
// 1. Retrieve the image size using ImgApp.
const file = DriveApp.getFileById(fileId);
const blob = file.getBlob();
const size = ImgApp.getSize(blob);
// 2. Create new Google Slides with the custom page size using DocsServiceApp.
const object = {
title: "sample title", // Title of created Slides.
width: { unit: "pixel", size: size.width },
height: { unit: "pixel", size: size.height },
};
const presentationId = DocsServiceApp.createNewSlidesWithPageSize(object);
// 3. Put the image and text.
const s = SlidesApp.openById(presentationId);
const slide = s.getSlides()[0];
slide.insertImage(blob);
slide.insertTextBox(text.text, text.left, text.top, text.width, text.height).getText().getTextStyle().setFontSize(text.fontSize);
s.saveAndClose();
// 4. Export the result image.
const obj = Slides.Presentations.Pages.getThumbnail(
presentationId,
slide.getObjectId(),
{ "thumbnailProperties.thumbnailSize": "LARGE", "thumbnailProperties.mimeType": "PNG" }
);
const url = obj.contentUrl.replace(/=s\d+/, "=s" + size.width);
const resultBlob = UrlFetchApp.fetch(url).getBlob().setName("Result_" + file.getName());
sheet.getImages()[0].remove();
sheet.insertImage(resultBlob, 2, 1);
DriveApp.getFileById(presentationId).setTrashed(true);
}
When this script is run, the following result is obtained.
Upvotes: 2