I've been googling this a lot but I couldn't find any answer.
I have a sheet like this:
| ID | Name | X Coord. | Y Coord. |
I have a custom picture (it's basically a map) 512x512 and I need to plot the points based on the X/Y Coords.
Is there any way to do that on Google Sheet? I'm open to any possible solution (even the ones including html)
Thanks in advance!
EDIT.This is what I need:
My sheet
| ID | Name | X Coord. | Y Coord. |
| 1 | AAA | 0 | 0 |
| 2 | BBB | 512 | 512 |
| 3 | CCC | 256 | 256 |
My picture
I believe your current situation and your goal as follows.
Unfortunately, in the current stage, there are no methods for directly editing the image and putting the texts and shapes in the methods for Google Apps Script. So in this case, it is required to use the workaround.
Fortunately, I have published a report about "Inserting Text on Image using Google Apps Script" in my blog. Ref I thought that in your situation, this might be able to be used as a workaround. The flow of this workaround is as follows.
In this case, it supposes that you have already had the Google Apps Script project. If you have no Google Apps Script project, please create it. For example, you can also create it from the script editor on Google Spreadsheet.
Please install the following Google Apps Script libraries. You can see the official document for installing the library at here.
Please enable Google Slides API at Advanced Google services.
Please copy and paste the following script to the script editor of Google Apps Script and please set the variables.
function myFunction() {
const fileIdOfImage = "###"; // Please set the fileID of image.
const spreadsheetId = "###"; // Please set the Spreadsheet ID of image.
const sheetName = "Sheet1"; // Please set the sheet name.
const outputFilename = "sample.png"; // Please set the output image filename.
// 1. Retrieve blob from an image file.
const blob = DriveApp.getFileById(fileIdOfImage).getBlob();
// 2. Retrieve the image size.
const { width, height } = ImgApp.getSize(blob);
// 3. Create new Google Slides with the page size which is the same with the image size.
const object = { title: "temp", width: { unit: "pixel", size: width }, height: { unit: "pixel", size: height } };
const id = DocsServiceApp.createNewSlidesWithPageSize(object);
// 4. Insert the image.
const s = SlidesApp.openById(id);
const slide = s.getSlides()[0];
const image = slide.insertImage(blob);
// 5. Retrieve the coordinates from Spreadsheet.
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
const values = sheet.getRange("B2:D" + sheet.getLastRow()).getValues();
// 6. Plot the points using the coordinates to Google Slides.
const pointSize = 8;
const fontSize = 10;
const boxSize = 60;
values.forEach(([v, x, y]) => {
let px = (x / 1.33333) - (pointSize / 2);
let py = (-1 * (y - height) / 1.33333) - (pointSize / 2);
slide.insertShape(SlidesApp.ShapeType.ELLIPSE, px, py, pointSize, pointSize).getFill().setSolidFill("#ff0000");
const textBox = slide.insertTextBox(v, px - (boxSize / 2) + (pointSize / 2), py - (boxSize / 2) + (pointSize / 2), boxSize, boxSize);
textBox.setContentAlignment(SlidesApp.ContentAlignment[(px < 0 && py > 0) || (px > 0 && py > 0) ? "TOP" : "BOTTOM"]);
const text = textBox.getText();
text.getParagraphStyle().setParagraphAlignment(SlidesApp.ParagraphAlignment[(px > 0 && py < 0) || (px > 0 && py > 0) ? "START" : "END"]);
text.getTextStyle().setFontSize(fontSize);
});
slide.group([image, ...slide.getShapes()]);
s.saveAndClose();
// 7. Retrieve the image blob from Google Slides.
const obj = Slides.Presentations.Pages.getThumbnail(id, slide.getObjectId(), {"thumbnailProperties.thumbnailSize": "LARGE"});
const outputBlob = UrlFetchApp.fetch(obj.contentUrl.replace("=s1600", "=s" + width)).getBlob().setName(outputFilename);
DriveApp.createFile(outputBlob);
DriveApp.getFileById(id).setTrashed(true);
}
fileIdOfImage
, and retrieve the coordinates for plotting the points from Spreadsheet of spreadsheetId
and sheetName
. And then, those are merged and output as an image file of outputFilename
.fontSize
, pointSize
and boxSize
.This image size is 512 x 512 pixels.
When above input data is used with above sample script, the following result is obtained. The origin of this plot is the lower left. This is from your sample image.