Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAxObject excel Sort Column
Forum Updated to NodeBB v4.3 + New Features

QAxObject excel Sort Column

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 602 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Allllex
    wrote on 18 Jul 2019, 20:21 last edited by
    #1

    Follow this example and other google example, i manage to perform the basic operation about an excel sheet. However, i cannot find a method to perform a "Sort Column" action.

    In VB , it's something like

    Range("a1:a10").Sort Key1:=Range("a1") 
    

    Does anyone know how I can write a SortColumn() via QAxObject ?

    1 Reply Last reply
    0
    • H Online
      H Online
      hskoglund
      wrote on 19 Jul 2019, 08:03 last edited by
      #2

      Hi, it's (almost :-) the same when using QAxObject, try something like this:

      auto randomName = []
      {
          QString s,c;
          do
          {
              c = QString("abcdehijkmoyprsu").mid(rand() % 20,1);
              s += c;
          } while (!c.isEmpty() || s.isEmpty());
          return s;
      };
      
      auto excel     = new QAxObject("Excel.Application");
      auto workbooks = excel->querySubObject("Workbooks");
      auto workbook  = workbooks->querySubObject("Add");
      auto sheets    = workbook->querySubObject("Worksheets");
      auto sheet     = sheets->querySubObject("Item(int)",1);
      
      qDebug() << "Fill column with random chars";
      for (int r = 1; (r <= 10); ++r)
          sheet->querySubObject("Cells(int,int)",r,1)->setProperty("Value",randomName());
      
      qDebug() << "Before sorting";
      for (int r = 1; (r <= 10); ++r)
          qDebug() << sheet->querySubObject("Cells(int,int)",r,1)->dynamicCall("Value()");
      
      // do the sort
      sheet->querySubObject("Range(a1:a10)")->dynamicCall("Sort(Key1:=QAxObject*)",sheet->querySubObject("Range(a1)")->asVariant());
      
      qDebug() << "After sorting";
      for (int r = 1; (r <= 10); ++r)
          qDebug() << sheet->querySubObject("Cells(int,int)",r,1)->dynamicCall("Value()");
      
      excel->dynamicCall("Quit()");
      
      1 Reply Last reply
      5
      • A Offline
        A Offline
        Allllex
        wrote on 20 Jul 2019, 07:31 last edited by
        #3

        Thanks a lot!!! This is the syntax I'm looking for!

        sheet->querySubObject("Range(a1:a10)")->dynamicCall("Sort(Key1:=QAxObject*)",sheet->querySubObject("Range(a1)")->asVariant());
        

        I wonder for other similar operation, we still have to look to MS's VBA documentation. And then translate/mirror to QT, am i right? Is there any qt doc about the possible or common parameters/candidates for members like

        dynamicCall(const char *, QList<QVariant> &) : QVariant
        
        J 1 Reply Last reply 20 Jul 2019, 08:05
        0
        • A Allllex
          20 Jul 2019, 07:31

          Thanks a lot!!! This is the syntax I'm looking for!

          sheet->querySubObject("Range(a1:a10)")->dynamicCall("Sort(Key1:=QAxObject*)",sheet->querySubObject("Range(a1)")->asVariant());
          

          I wonder for other similar operation, we still have to look to MS's VBA documentation. And then translate/mirror to QT, am i right? Is there any qt doc about the possible or common parameters/candidates for members like

          dynamicCall(const char *, QList<QVariant> &) : QVariant
          
          J Offline
          J Offline
          JonB
          wrote on 20 Jul 2019, 08:05 last edited by JonB
          #4

          @Allllex
          I haven't used QAxObject, so I hope I'm not speaking out of turn, but, yes, for calling VBA-type stuff from Qt or other frameworks (which I have done) I do expect to have to refer to VBA docs. There are so many possibilities, it's not really up to the calling framework to document what you can do, in my experience you have to find VBA docs/examples and adapt to your particular wrapper functions.

          A 1 Reply Last reply 21 Jul 2019, 13:03
          1
          • J JonB
            20 Jul 2019, 08:05

            @Allllex
            I haven't used QAxObject, so I hope I'm not speaking out of turn, but, yes, for calling VBA-type stuff from Qt or other frameworks (which I have done) I do expect to have to refer to VBA docs. There are so many possibilities, it's not really up to the calling framework to document what you can do, in my experience you have to find VBA docs/examples and adapt to your particular wrapper functions.

            A Offline
            A Offline
            Allllex
            wrote on 21 Jul 2019, 13:03 last edited by
            #5

            @JonB

            Seems you confirm what i expect.
            I will look up VBA docs. Thanks!

            1 Reply Last reply
            0

            1/5

            18 Jul 2019, 20:21

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved