My script is building a chart on an Excel file.
It works perfectly when running manually, the following chart is created correctly:
But when executed from Power Automate (SharePoint library), the chart is wrong:
This is very intriguing… It is the same script.
Can anyone help?
EDIT:
This is the step calling the Office Script from a SharePoint library:
And this is the script:
<code> function main(workbook: ExcelScript.Workbook, idioma: string = "en") {
let calculations = workbook.getWorksheet("Calculations");
let charts = workbook.getWorksheet("Charts");
let cenario = workbook.getNamedItem("cenario").getValue();
const estilo: number = 5;
// ----------------- Gráfico de Linhas Monte Carlo --------------------
let montecarlo = workbook.getWorksheet("MonteCarlo");
// ----------------- Gráfico combinado --------------------
// define os intervalos de dados
let rngAnos = montecarlo.getRange("E8:E40");
let rngDados_1 = montecarlo.getRange("G8:G40");
// cria o primeiro gráfico combinado
let chart_1 = charts.addChart(ExcelScript.ChartType.columnClustered, rngDados_1);
chart_1.getAxes().getCategoryAxis().setCategoryNames(rngAnos); // acrescenta o eixo horizontal
//chart_1.addChartSeries(translate(idioma, "lblMargem"), 1).setValues(rngDados_2);
// séries
let serie1 = chart_1.getSeries()[0];
let serie2 = chart_1.getSeries()[1];
serie1.setName(translate(idioma, "lblDesvioP"));
// título
chart_1.getTitle().setText(translate(idioma, "lblMonteCarlo"));
// legenda
chart_1.getLegend().setVisible(true);
chart_1.getLegend().setPosition(ExcelScript.ChartLegendPosition.bottom);
// eixo horizontal
let min_val = chart_1.getAxes().getValueAxis().getMinimum();
chart_1.getAxes().getValueAxis().setPositionAt(min_val);
// posição do gráfico
positionChart(chart_1, "B68", 820, 300);
chart_1.setStyle(estilo);
console.log("Gráfico criado")
};
// Função para o eixo horizontal no mínimo
function setChartAxisMinimum(chart: ExcelScript.Chart) {
let min_val = chart.getAxes().getValueAxis().getMinimum();
chart.getAxes().getValueAxis().setPosition(min_val);
}
// Função para posicionamento do gráfico
function positionChart(chart: ExcelScript.Chart, cellAddress: string, width: number, height: number) {
let sheet = chart.getWorksheet();
let cell = sheet.getRange(cellAddress);
// Set the chart position
chart.setTop(cell.getTop());
chart.setLeft(cell.getLeft());
// Set the size of the chart
chart.setHeight(height); // Height in points
chart.setWidth(width); // Width in points
};
// Função para obtenção de range de dados a partir de name
function getRangeByName(sheet: ExcelScript.Worksheet, rangeName: string): ExcelScript.Range {
let namedItem = sheet.getNamedItem(rangeName);
if (!namedItem) {
throw new Error(`Named range '${rangeName}' not found.`);
}
return namedItem.getRange().getCell(0, 6).getExtendedRange(ExcelScript.KeyboardDirection.right);
};
// Tradução português inglês
function translate(language: string, phrase: string): string {
const translations: { [key: string]: { [key: string]: string } } = {
pt: {
lblMonteCarlo: "Simulação Monte Carlo",
lblDesvioP: "Desvio-padrão"
},
en: {
lblMonteCarlo: "Monte Carlo simulation",
lblDesvioP: "Standard deviation"
}
};
if (translations[language]) {
return translations[language][phrase] || "Translation not available";
} else {
return "Language not supported";
}
};
</code>
<code> function main(workbook: ExcelScript.Workbook, idioma: string = "en") {
let calculations = workbook.getWorksheet("Calculations");
let charts = workbook.getWorksheet("Charts");
let cenario = workbook.getNamedItem("cenario").getValue();
const estilo: number = 5;
// ----------------- Gráfico de Linhas Monte Carlo --------------------
let montecarlo = workbook.getWorksheet("MonteCarlo");
// ----------------- Gráfico combinado --------------------
// define os intervalos de dados
let rngAnos = montecarlo.getRange("E8:E40");
let rngDados_1 = montecarlo.getRange("G8:G40");
// cria o primeiro gráfico combinado
let chart_1 = charts.addChart(ExcelScript.ChartType.columnClustered, rngDados_1);
chart_1.getAxes().getCategoryAxis().setCategoryNames(rngAnos); // acrescenta o eixo horizontal
//chart_1.addChartSeries(translate(idioma, "lblMargem"), 1).setValues(rngDados_2);
// séries
let serie1 = chart_1.getSeries()[0];
let serie2 = chart_1.getSeries()[1];
serie1.setName(translate(idioma, "lblDesvioP"));
// título
chart_1.getTitle().setText(translate(idioma, "lblMonteCarlo"));
// legenda
chart_1.getLegend().setVisible(true);
chart_1.getLegend().setPosition(ExcelScript.ChartLegendPosition.bottom);
// eixo horizontal
let min_val = chart_1.getAxes().getValueAxis().getMinimum();
chart_1.getAxes().getValueAxis().setPositionAt(min_val);
// posição do gráfico
positionChart(chart_1, "B68", 820, 300);
chart_1.setStyle(estilo);
console.log("Gráfico criado")
};
// Função para o eixo horizontal no mínimo
function setChartAxisMinimum(chart: ExcelScript.Chart) {
let min_val = chart.getAxes().getValueAxis().getMinimum();
chart.getAxes().getValueAxis().setPosition(min_val);
}
// Função para posicionamento do gráfico
function positionChart(chart: ExcelScript.Chart, cellAddress: string, width: number, height: number) {
let sheet = chart.getWorksheet();
let cell = sheet.getRange(cellAddress);
// Set the chart position
chart.setTop(cell.getTop());
chart.setLeft(cell.getLeft());
// Set the size of the chart
chart.setHeight(height); // Height in points
chart.setWidth(width); // Width in points
};
// Função para obtenção de range de dados a partir de name
function getRangeByName(sheet: ExcelScript.Worksheet, rangeName: string): ExcelScript.Range {
let namedItem = sheet.getNamedItem(rangeName);
if (!namedItem) {
throw new Error(`Named range '${rangeName}' not found.`);
}
return namedItem.getRange().getCell(0, 6).getExtendedRange(ExcelScript.KeyboardDirection.right);
};
// Tradução português inglês
function translate(language: string, phrase: string): string {
const translations: { [key: string]: { [key: string]: string } } = {
pt: {
lblMonteCarlo: "Simulação Monte Carlo",
lblDesvioP: "Desvio-padrão"
},
en: {
lblMonteCarlo: "Monte Carlo simulation",
lblDesvioP: "Standard deviation"
}
};
if (translations[language]) {
return translations[language][phrase] || "Translation not available";
} else {
return "Language not supported";
}
};
</code>
function main(workbook: ExcelScript.Workbook, idioma: string = "en") {
let calculations = workbook.getWorksheet("Calculations");
let charts = workbook.getWorksheet("Charts");
let cenario = workbook.getNamedItem("cenario").getValue();
const estilo: number = 5;
// ----------------- Gráfico de Linhas Monte Carlo --------------------
let montecarlo = workbook.getWorksheet("MonteCarlo");
// ----------------- Gráfico combinado --------------------
// define os intervalos de dados
let rngAnos = montecarlo.getRange("E8:E40");
let rngDados_1 = montecarlo.getRange("G8:G40");
// cria o primeiro gráfico combinado
let chart_1 = charts.addChart(ExcelScript.ChartType.columnClustered, rngDados_1);
chart_1.getAxes().getCategoryAxis().setCategoryNames(rngAnos); // acrescenta o eixo horizontal
//chart_1.addChartSeries(translate(idioma, "lblMargem"), 1).setValues(rngDados_2);
// séries
let serie1 = chart_1.getSeries()[0];
let serie2 = chart_1.getSeries()[1];
serie1.setName(translate(idioma, "lblDesvioP"));
// título
chart_1.getTitle().setText(translate(idioma, "lblMonteCarlo"));
// legenda
chart_1.getLegend().setVisible(true);
chart_1.getLegend().setPosition(ExcelScript.ChartLegendPosition.bottom);
// eixo horizontal
let min_val = chart_1.getAxes().getValueAxis().getMinimum();
chart_1.getAxes().getValueAxis().setPositionAt(min_val);
// posição do gráfico
positionChart(chart_1, "B68", 820, 300);
chart_1.setStyle(estilo);
console.log("Gráfico criado")
};
// Função para o eixo horizontal no mínimo
function setChartAxisMinimum(chart: ExcelScript.Chart) {
let min_val = chart.getAxes().getValueAxis().getMinimum();
chart.getAxes().getValueAxis().setPosition(min_val);
}
// Função para posicionamento do gráfico
function positionChart(chart: ExcelScript.Chart, cellAddress: string, width: number, height: number) {
let sheet = chart.getWorksheet();
let cell = sheet.getRange(cellAddress);
// Set the chart position
chart.setTop(cell.getTop());
chart.setLeft(cell.getLeft());
// Set the size of the chart
chart.setHeight(height); // Height in points
chart.setWidth(width); // Width in points
};
// Função para obtenção de range de dados a partir de name
function getRangeByName(sheet: ExcelScript.Worksheet, rangeName: string): ExcelScript.Range {
let namedItem = sheet.getNamedItem(rangeName);
if (!namedItem) {
throw new Error(`Named range '${rangeName}' not found.`);
}
return namedItem.getRange().getCell(0, 6).getExtendedRange(ExcelScript.KeyboardDirection.right);
};
// Tradução português inglês
function translate(language: string, phrase: string): string {
const translations: { [key: string]: { [key: string]: string } } = {
pt: {
lblMonteCarlo: "Simulação Monte Carlo",
lblDesvioP: "Desvio-padrão"
},
en: {
lblMonteCarlo: "Monte Carlo simulation",
lblDesvioP: "Standard deviation"
}
};
if (translations[language]) {
return translations[language][phrase] || "Translation not available";
} else {
return "Language not supported";
}
};
2