[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Re: Object Model for ODF documents
From:       Stefan Nikolaus <stefan.nikolaus () kdemail ! net>
Date:       2006-09-29 16:40:05
Message-ID: 200609291840.13560.stefan.nikolaus () kdemail ! net
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


On Friday 29 September 2006 16:19, Inge Wallin wrote:
> > It would be interesting to avoid the use of hard-coded cell references in
> > the code.  Some spreadsheets allow the end-user to name cells or cell
> > ranges with descriptive names.  Does KSpread allow this?  If so, A7 could
> > be named "Name" and A8  called "Address", etc.   Then code like
> > sheet1.cell("Name").setText("Joe User").
> >
> > Something like that would allow the code to be more independent of the
> > spreadsheet.
>
> I agree.  KSpread 1.6 doesn't support named cells, but it does support
> named areas, so this problem can be worked around with a setNamedCell(name,
> value) function written in python.

Patch attached.
The method KSpreadCore::Sheet::namedCell returns the top left cell of a named 
area. Unfortunately, an overloading of the KSpreadCore::Sheet::cell method is 
not possible.

Regards,
Stefan

["temp.diff" (text/x-diff)]

Index: kspread_doc.h
===================================================================
--- kspread_doc.h	(Revision 590253)
+++ kspread_doc.h	(Arbeitskopie)
@@ -521,9 +521,8 @@
 
   void changeAreaSheetName(const QString & oldName,const QString &sheetName);
 
+  QRect namedArea(const QString& name, const QString& sheetName);
 
-  QRect getRectArea(const QString &  _sheetName);
-
   /**
    * Inserts an object to the object list.
    */
Index: plugins/scripting/kspreadcore/krs_sheet.cpp
===================================================================
--- plugins/scripting/kspreadcore/krs_sheet.cpp	(Revision 590253)
+++ plugins/scripting/kspreadcore/krs_sheet.cpp	(Arbeitskopie)
@@ -25,6 +25,8 @@
 #include <kspread_sheet.h>
 #include <kspread_cell.h>
 #include <kspread_value.h>
+#include <kspread_view.h>
+#include <region.h>
 
 namespace Kross { namespace KSpreadCore {
 
@@ -39,6 +41,7 @@
     this->addFunction0< Cell >("firstCell", this, &Sheet::firstCell);
 
     this->addFunction2< Cell, Kross::Api::Variant, Kross::Api::Variant >("cell", \
this, &Sheet::cell); +    this->addFunction1< Cell, Kross::Api::Variant \
>("namedCell", this, &Sheet::namedCell);  
     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("insertRow", \
                this, &Sheet::insertRow);
     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("insertColumn", \
this, &Sheet::insertColumn); @@ -83,6 +86,13 @@
     return new Cell(m_sheet->cellAt(c,r),m_sheet,c,r);
 }
 
+Cell* Sheet::namedCell(const QString& name) {
+    const QRect rect = m_doc->namedArea( name, m_sheet->sheetName() );
+    if ( rect.isNull() ) return 0;
+    const QPoint location = rect.topLeft();
+    return new Cell(m_sheet->cellAt(location),m_sheet,location.x(),location.y());
+}
+
 bool Sheet::insertRow(uint row) {
     return m_sheet->insertRow(row);
 }
Index: plugins/scripting/kspreadcore/krs_sheet.h
===================================================================
--- plugins/scripting/kspreadcore/krs_sheet.h	(Revision 590253)
+++ plugins/scripting/kspreadcore/krs_sheet.h	(Arbeitskopie)
@@ -112,6 +112,11 @@
 	 */
 	Cell* cell(uint col, uint row);
 
+        /**
+         * \return the top left cell of the area named \p name
+         */
+	Cell* namedCell(const QString& name);
+
 	/**
 	 * Add a new row.
 	 */
Index: kspread_doc.cc
===================================================================
--- kspread_doc.cc	(Revision 590253)
+++ kspread_doc.cc	(Arbeitskopie)
@@ -1975,17 +1975,17 @@
         }
 }
 
-QRect Doc::getRectArea(const QString  &_sheetName)
+QRect Doc::namedArea( const QString& name, const QString& sheetName )
 {
-  QValueList<Reference>::Iterator it2;
-  for ( it2 = d->refs.begin(); it2 != d->refs.end(); ++it2 )
+    QValueList<Reference>::ConstIterator it( d->refs.constBegin() );
+    for ( ; it != d->refs.constEnd(); ++it )
+    {
+        if ( (*it).ref_name == name && (*it).sheet_name == sheetName )
         {
-        if((*it2).ref_name==_sheetName)
-                {
-                return (*it2).rect;
-                }
+            return (*it).rect;
         }
-  return QRect(-1,-1,-1,-1);
+    }
+    return QRect();
 }
 
 QDomElement Doc::saveAreaName( QDomDocument& doc )


[Attachment #8 (application/pgp-signature)]

_______________________________________________
koffice-devel mailing list
koffice-devel@kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic