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.
  • AllllexA Offline
    AllllexA Offline
    Allllex
    wrote on 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
    • hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on 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
      • AllllexA Offline
        AllllexA Offline
        Allllex
        wrote on 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
        
        JonBJ 1 Reply Last reply
        0
        • AllllexA Allllex

          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
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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.

          AllllexA 1 Reply Last reply
          1
          • JonBJ JonB

            @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.

            AllllexA Offline
            AllllexA Offline
            Allllex
            wrote on last edited by
            #5

            @JonB

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

            1 Reply Last reply
            0

            • Login

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