Excel office script not running correctly from Power Automate

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật