How to remove ApiImage from ApiWorksheet?

I’m preparing a plugin. I use document builder to insert ApiImages into the ApiWorksheet. I have two questions:

  1. Is it possible to identify a previously added Apiimage by some identifier among other ApiImage’s?
  2. Is it possible to remove an ApiImage from a ApiWorksheet using document builder?

Greetings Piotr!

Hello @Piotr

Please specify how exactly images are getting inserted in your case.

Thank you for your message!
As I wrote earlier, I prepare a plugin and use document builder to manipulate the sheet. In the Asc.scope.plots array I have previously made plots using Google Charts, which are converted to Base64 encoded images.

function pushCharts() {    
    window.Asc.plugin.callCommand( function () {
        //adding new ApiImage's...
        let sheet = Api.GetSheet(Asc.scope.sheet);
        for (let i=0; i<Asc.scope.plots.length; i++) {
            let plot = Asc.scope.plots[i];
            sheet.AddImage(plot, 120*36000, 90*36000, i+1, 20*36000, i+1, 20*36000);
    }, false, true);

Now I would like to add code to this function that will allow me to delete existing images before adding new, current ones.

function deleteOldAndPushCharts() {    
    window.Asc.plugin.callCommand( function () {
        let sheet = Api.GetSheet(Asc.scope.sheet);
        //delete all old ApiImage's
        //adding new ApiImage's...        
        for (let i=0; i<Asc.scope.plots.length; i++) {
            let plot = Asc.scope.plots[i];
            sheet.AddImage(plot, 120*36000, 90*36000, i+1, 20*36000, i+1, 20*36000);
    }, false, true);    

Thank you in advance for your help!
greetings Piotr

Unfortunately, direct method to remove images from worksheet does not exist. I think you can directly replace those images with ReplaceCurrentImage method. However, you will have to manually select every image before replacing it.

Alternative solution is to ‘re-create’ a sheet without images and then insert new ones. An example is:

var oWorksheet = Api.GetActiveSheet();
Api.AddSheet("New sheet");
var oSheet = Api.GetSheet("New sheet");
var oRange = oWorksheet.GetRange("A1:J25");

oSheet.AddImage("", 120 * 36000, 70 * 36000, 10, 2 * 36000, 0, 3 * 36000);

Here new sheet is created, data copied to it (range of cells without images), new images are pasted into new sheet, old sheet is removed and then additionally new sheet is renamed.
Methods used: GetActiveSheet, AddSheet, GetSheet, SetActive, GetRange, Copy, Delete, AddImage, SetName.

Note: as of now I’d recommend to avoid using Delete() on original sheet before making sure that data is copied correctly. You can create a copy of original sheet with images to run tests with it or comment out this line until proper result is achieved.
In version 8.1 various changes will be made to overall work with sheets that will improve interactions with certain methods, including Delete().

Thank you for your answer.

By the way:

  • When do you plan to release version 8.1?
  • In addition to implementing the Delete() method for the ApiImage class, are you planning to add the same method for the ApiChart class?
  • Will it be possible to add series for the second Y axis?

Greetings Piotr.

Currently estimated date of release is summer of this year.

Please note that these questions are not related to the topic of this thread. Please create a separate thread to avoid mixing up different topics in one thread.