Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

copy sheet object from one sheet to another

Hi QV experts,

     How do I copy object from one sheet to another sheet using QV API. Please suggest.

Thanks in advance

8 Replies
johnw
Champion III
Champion III

The attached example is from version 9 - perhaps there is a better way in version 10 or 11.  It exports the object to an xml file, then imports to create another chart.  I got the basic code from Rob Wunderlich, and then worked from there.  The attached example also does more than you asked for, and doesn't put the export/import in the same macro, as I wanted to export once, then import many times.  Hopefully the comments are sufficient to figure out what you need and don't need from the example.

Not applicable
Author

Hi John,

Thank you for your reply. My requirement is to copy charts(more than 1 type like combo,straight table etc)  from sheet1 to sheet2, sheet3 and so on. Export import will help me to do this, but the challenge is "How do I  identify(Object id)  the equivalent chart from sheet -1 say Straight table chart with Object ID CH01 in sheet2 and sheet3 and so on. I just need to know how do I get hold of object ID 's

Thanks in advance

Not applicable
Author

I can't understand ur requirement clearly but as per my understanding am giving one solution. To copy a sheet object from one sheet to another,first select the sheet object which u want to copy then right click on that object and select clone,after selecting clone,detach that object,then drag and drop that object to the sheet which u want to show and in that sheet attach that object. if u  right click on the object u ll get the clone,attach and detach option. hope this s ur need.

Warm Regards,

Arun Prasadh.N.

johnw
Champion III
Champion III

padmasali wrote:

...How do I  identify(Object id)  the equivalent chart from sheet -1 say Straight table chart with Object ID CH01 in sheet2 and sheet3 and so on. I just need to know how do I get hold of object ID 's...


The example I gave shows one way to identify the newly-added chart ID.  It's annoyingly complicated, and there should be a better way, but I couldn't find one in version 9.  Maybe there's something easier in version 10 or 11.

Edit:  Here's the relevant section of the macro code:

objects = sheet.GetSheetObjects 'get array of objects on sheet before adding new chart

beforeMax = ubound(objects)

dim objectIDBefore(100) 'runs slightly faster if first set up a simple array

for j = 0 to beforeMax

    objectIDBefore(j) = objects(j).GetObjectID

next

sheet.CreateObjectFromXMLPropertiesFile "DynamicChartBase.xml" 'chreate chart from XML file

objects = sheet.GetSheetObjects               'get array of objects now on the sheet

for i = beforeMax + 1 to 0 step -1            'for each object now on the sheet

    objectID = objects(i).GetObjectID

    for j = 0 to beforeMax                    'check all objects that WERE on the sheet

        if  objectIDBefore(j) = objectID then 'if the object was already on the sheet

            j = 9999                          'exit the inner loop

        end if

    next

    if  j < 9999 then                         'if the object is new

        set chart = objects(i)                'get a handle to the object

        i = 0                                 'and exit the outer loop

    end if

next

chartVariable.SetContent objectID,false       'store the new chart's ID

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

I've always found that the new object is added to the head of the sheet object collection.

http://community.qlik.com/message/171082#171082

-Rob

johnw
Champion III
Champion III

Ah, yes, I remember that now.  I'm trying to refresh my memory of our conversations regarding this and why I'm not using it, and found this old thread:

http://community.qlik.com/message/108059#108059

John: "set chart = sheetObjects(uBound(sheetObjects))

This appears to always give me the new chart. But does anyone know if I can count on that?"

Rob: "It almost always works for me, but like you I've never seen any doc that promises it will continue to work. The only time I've seen the ubound technique not work is if a doc is saved with a 9+ version, it will not work if used with 8.5."

John: "OK. Since it doesn't seem to be documented, and the functionality here has changed over time, we can't count on the ubound() technique continuing to work in the future. Also, it would be nice to have a template that works for earlier versions of QlikView for people who haven't upgraded. I'll implement a loop to compare before and after to get the right ID, and post when I have that working."

So I guess my conclusion would be use the ubound() technique if you want something simple that will likely work just fine and you're on at least version 9.  Loop if you want to be sure it will keep working.  Mind you, QlikTech could change functionality for something used in the loop just like they cound change how objects are added, so even the loop isn't a guarantee.

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

I'll agree that your solution is more robust. Thanks for digging that up.

-Rob

Not applicable
Author

padmasali wrote:

Hi QV experts,

     How do I copy object from one sheet to another sheet using QV API. Please suggest.

Thanks in advance

  I do it like that:

  works in v 11

function copyChart () {

       // Get reference on existing Object

       refChart = ActiveDocument.GetSheetObject("MY_CHART");

      // Create Empty Object, doesnt matter which type to create

       newChart = ActiveDocument.GetSheetByID("SH26").CreateBarChart();

       // Get properties of existing Object

      cp = refChart.GetProperties();

      // Set ID of new object, otherwise it will create linked object

      cp.GraphLayout.Frame.ObjectId = newChart.GetObjectId();

      // Set Properties of New Chart

      newChart.SetProperties(cp);

}


Best regards,
Ruslan