Excel script to export chart in sharepoint

daves1

New Member
Joined
Jan 27, 2021
Messages
1
Office Version
  1. 365
Platform
  1. MacOS
hello to all.
I am trying to create a script that extracts chart elements (in jpg) from excel to sharepoint.
I'm currently using this script but without results:
Excel Formula:
function main(workbook: ExcelScript.Workbook): ReportImages {

  workbook.getApplication().calculate(ExcelScript.CalculationType.full);

  let sheet1 = workbook.getWorksheet("Sheet1");


  const table = workbook.getWorksheet('InvoiceAmounts').getTables()[0];


  const rows = table.getRange().getTexts();

  const selectColumns = rows.map((row) => {


    return [row[2], row[5]];


  });


  table.setShowTotals(true);


  selectColumns.splice(selectColumns.length-1, 1);


  console.log(selectColumns);


  workbook.getWorksheet('ChartSheet')?.delete();


  const chartSheet = workbook.addWorksheet('ChartSheet');


  const targetRange = updateRange(chartSheet, selectColumns);


  // Insert chart on sheet 'Sheet1'


  let chart_2 = chartSheet.addChart(ExcelScript.ChartType.columnClustered, targetRange);


  chart_2.setPosition('D1');


  const chartImage = chart_2.getImage();


  const tableImage = table.getRange().getImage();


  return {


    chartImage,


    tableImage


  }


}


function updateRange(sheet: ExcelScript.Worksheet, data: string[][]): ExcelScript.Range {


  const targetRange = sheet.getRange('A1').getResizedRange(data.length-1, data[0].length-1);


  targetRange.setValues(data);

  return targetRange;

}

interface ReportImages {

  chartImage: string
  tableImage: string
}
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Can you share a bit more about how you're planning to use the extracted image? Do you plan to manually save those extracted image first then upload to SharePoint? Or do you plan to build a Power Automate flow to integrate your script with some SharePoint action to automatically upload the extracted image to SharePoint?

Range.getImage or Chart.getImage returns base64-encoded PNG image data. If you want to see that picture, you can prefix "data:image/png;base64," to the result returned from the getImage API to build a "data URL". You can then paste this data URL into the web browser address bar and hit enter to show the image. You can further save that image to you local disk and upload to SharePoint - if manual process is what you needed.

Here is one sample script that can extract the image of a chart into a data URL:
JavaScript:
function main(workbook: ExcelScript.Workbook)
{
  const chart = workbook.getWorksheet("Sheet1").getChart("Chart 1");
  const chartImage = chart.getImage(512);
  const result = `data:image/png;base64,${chartImage}`;
  console.log(result);
}

Run this script, copy the data URL from the output window and paste into web browser address bar.

The Power Automate approach is a bit more complicated. If you're interested, you can take a look at these articles:

Hope this helps!

- Yutao
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top