Only graphic images can be made with Google AppScript. I looked at a lot of code, but they didn't work for me; either they were making a PDF then converting it to a picture, or they were sending it to a slide and then making a picture.
My aim was to make a picture directly with its formats. I converted pictures with their formats, but when the picture was taken directly from the graphic, it was colorless.
Here I’ve converted a friend's code that converts the HTML table to a picture from there.
get the table
var aa = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Holiday");
active column
stun=aa.getActiveRange().getColumn();
active column width
stungen=aa.getColumnWidth(stun)+70
number of filled rows
var count = ss.getDisplayValues().flat().filter(String).length;
column and row to import
var sa =aa.getRange(1,stun,count);
formats of received data
var data = sa.getDisplayValues();
var background = sa.getBackgrounds();
var fontColor = sa.getFontColors();
var fontStyles = sa.getFontStyles();
var fontWeight = sa.getFontWeights();
var fontSize = sa.getFontSizes();
Making data into a formatted html table
for (var i = 0; i < data.length; i++) {
html += "<tr>"
for (var j = 0; j < data[i].length; j++) {
html += "<td style='height:20px;background:" + background[i][j] + ";color:" + fontColor[i][j] + ";font-style:" + fontStyles[i][j] + ";font-weight:" + fontWeight[i][j] + ";font-size:" + (fontSize[i][j] + 6) + "px;'>" + data[i][j] + "</td>";
}
html += "</tr>";
}
html + "</table>"
convert html table to chart
const img = Charts.newTableChart().setDataTable(Charts.newDataTable().addColumn(Charts.ColumnType.STRING, '').addRow([html]).build()).setOption('allowHtml',true).setDimensions(stungen,count*28).build();
the size of the picture
setDimensions(stungen,count*28)
stungen=column width
convert graphic to image and get link
var imageData1 = Utilities.base64Encode(img.getAs('image/png').getBytes());
var imageUrl = "data:image/png;base64," + encodeURI(imageData1);
htmlOutput.append("Holiday Listesi <br/>");
show image in dialog
htmlOutput.append("Holiday Listesi <br/>");
htmlOutput.append("<img border=\"1\" src=\"" + imageUrl + "\">");
SpreadsheetApp.getUi().showModelessDialog(htmlOutput,"demo");
return htmlOutput;
}
the whole code
function drawTable() {
var aa = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Holiday");
stun=aa.getActiveRange().getColumn();
stungen=aa.getColumnWidth(stun)+70
var sa =aa.getRange(1,stun);
var lastRow = aa.getLastRow();
var ss =aa.getRange(1,stun,lastRow);
var count = ss.getDisplayValues().flat().filter(String).length;
var sa =aa.getRange(1,stun,count);
var data = sa.getDisplayValues();
var background = sa.getBackgrounds();
var fontColor = sa.getFontColors();
var fontStyles = sa.getFontStyles();
var fontWeight = sa.getFontWeights();
var fontSize = sa.getFontSizes();
var html = "<b>HOLİDAY</b>"+ "<table border='1'>";
for (var i = 0; i < data.length; i++) {
html += "<tr>"
for (var j = 0; j < data[i].length; j++) {
html += "<td style='height:20px;background:" + background[i][j] + ";color:" + fontColor[i][j] + ";font-style:" + fontStyles[i][j] + ";font-weight:" + fontWeight[i][j] + ";font-size:" + (fontSize[i][j] + 6) + "px;'>" + data[i][j] + "</td>";
}
html += "</tr>";
}
html + "</table>"
var htmlOutput = HtmlService.createHtmlOutput();
const img = Charts.newTableChart().setDataTable(Charts.newDataTable().addColumn(Charts.ColumnType.STRING, '').addRow([html]).build()).setOption('allowHtml',true).setDimensions(stungen,count*28).build();
var imageData1 = Utilities.base64Encode(img.getAs('image/png').getBytes());
var imageUrl = "data:image/png;base64," + encodeURI(imageData1);
htmlOutput.append("Holiday Listesi <br/>");
htmlOutput.append("<img border=\"1\" src=\"" + imageUrl + "\">");
SpreadsheetApp.getUi().showModelessDialog(htmlOutput,"demo");
return htmlOutput;
}
Resim