MCPcopy Create free account
hub / github.com/analyticswithadam/App_Script / transferSpecificChartsToFirstSlide

Function transferSpecificChartsToFirstSlide

Sheets to Slides.js:1–64  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1function transferSpecificChartsToFirstSlide() {
2 // Open the Google Slides deck where you want to transfer the charts
3 var slidesDeck = SlidesApp.openById("1Ewx4_F84z4BIciNeOg3HpMMRAUs2NHz9_jG4Qp-NIeY");
4
5 // Open the active Google Sheet
6 var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
7
8 // Get all the charts in the sheet
9 var charts = sheet.getCharts();
10
11 // Ensure there are enough charts
12 if (charts.length < 4) {
13 SpreadsheetApp.getUi().alert('Not enough charts found in the sheet.');
14 return;
15 }
16
17 // Get the first slide
18 var slide = slidesDeck.getSlides()[0];
19
20 // Clear existing images on the slide except the header icon
21 var elements = slide.getPageElements();
22 elements.forEach(function(element) {
23 if (element.getPageElementType() === SlidesApp.PageElementType.IMAGE) {
24 var position = element.getLeft();
25 var size = element.getWidth();
26
27 // Assuming the header icon is smaller and positioned at the top, we keep it
28 if (position > 100 || size > 100) { // Adjust these thresholds based on your actual icon's position and size
29 element.remove();
30 }
31 }
32 });
33
34 // Define which chart goes where and its exact size on the slide
35 var chartMappings = [
36 { chartIndex: 2, left: 50, top: 100, width: 150, height: 100 }, // -> Top left
37 { chartIndex: 1, left: 350, top: 100, width: 150, height: 100 }, // -> Top right
38 { chartIndex: 0, left: 50, top: 200, width: 250, height: 150 }, // -> Bottom left
39 { chartIndex: 3, left: 350, top: 200, width: 250, height: 150 } // -> Bottom right
40 ];
41
42 /**
43 * This code iterates over each chart mapping in the chartMappings array. For each mapping, it:
44 Retrieves the corresponding chart from the charts array.
45 Converts the chart into a PNG image.
46 Inserts the image onto the slide at a specific position (left, top) and size (width, height) as defined in the chartMappings array.
47 The result is that each chart is placed on the slide exactly where and how you want it, based on the parameters defined in chartMappings.
48 */
49
50 chartMappings.forEach(function(mapping) {
51 var chart = charts[mapping.chartIndex];
52 var chartImage = chart.getAs('image/png');
53
54 // Insert the chart image into the slide at the specified position and size
55 slide.insertImage(chartImage)
56 .setLeft(mapping.left)
57 .setTop(mapping.top)
58 .setWidth(mapping.width)
59 .setHeight(mapping.height);
60 });

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected