1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
<!--
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
-->
<meta>
<topic id="SF_Dictionary" indexer="include" status="PUBLISH">
<title id="tit" xml-lang="en-US">ScriptForge.Dictionary service</title>
<filename>/text/sbasic/shared/03/sf_dictionary.xhp</filename>
</topic>
</meta>
<body>
<section id="abstract">
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id261582733781987">
<bookmark_value>Dictionary service</bookmark_value>
</bookmark>
<h1 id="hd_id731582733781114" xml-lang="en-US"><variable id="SFDictionary"><link href="text/sbasic/shared/03/sf_dictionary.xhp"><literal>ScriptForge</literal>.<literal>Dictionary</literal> service</link></variable></h1>
<paragraph role="paragraph" id="par_id891582884466217" xml-lang="en-US">A dictionary is a collection of key-item pairs</paragraph>
<list type="unordered">
<listitem><paragraph id="par_id861582884516571" role="listitem" xml-lang="en-US">The key is a case-insensitive string</paragraph></listitem>
<listitem><paragraph id="par_id531582884549542" role="listitem" xml-lang="en-US">Items may be of any type</paragraph></listitem>
</list>
</section>
<paragraph role="paragraph" id="par_id891582884593057" xml-lang="en-US">Keys and items can be retrieved, counted, updated, and much more.</paragraph>
<paragraph role="tip" id="par_id971582884636922" xml-lang="en-US">The Dictionary service is similar to the built-in %PRODUCTNAME Basic <literal>Collection</literal> object, however with more features. For example, <literal>Collection</literal> objects do not support the retrieval of keys. Moreover, Dictionaries provide additional capabilities as replacing keys, testing if a specific key already exists and converting the Dictionary into an Array object or JSON string.</paragraph>
<h2 id="hd_id581582885621841" xml-lang="en-US">Service invocation</h2>
<embed href="text/sbasic/shared/00000003.xhp#In_Basic"/>
<paragraph role="paragraph" id="par_id821610388789467">The following example creates <literal>myDict</literal> as an empty dictionary.</paragraph>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id851582897798335">GlobalScope.BasicLibraries.loadLibrary("ScriptForge")</paragraph>
<paragraph role="bascode" localize="false" id="bas_id371582885621964">Dim myDict As Variant</paragraph>
<paragraph role="bascode" localize="false" id="bas_id201582885621287">myDict = CreateScriptService("Dictionary")</paragraph>
</bascode>
<paragraph role="paragraph" id="par_id71158288562139" xml-lang="en-US">It is recommended to free resources after use:</paragraph>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id721582885621540">Set myDict = myDict.Dispose()</paragraph>
</bascode>
<embed href="text/sbasic/shared/00000003.xhp#In_Python"/>
<paragraph role="paragraph" id="par_id551626869252987">The example below creates an empty instance of the <literal>Dictionary</literal> service and uses the Python native <literal>update</literal> method to populate it with the contents of a Python <literal>dict</literal> object.</paragraph>
<pycode>
<paragraph role="pycode" localize="false" id="pyc_id61626869417885">dico = dict('A' = 1, 'B' = 2, 'C' = 3)</paragraph>
<paragraph role="pycode" id="pyc_id61626869417128"># Initialize myDict as an empty dict object</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id501626869418687">myDict = CreateScriptService('Dictionary')</paragraph>
<paragraph role="pycode" id="pyc_id921626869402748"># Load the values of dico into myDict</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id921626869418958">myDict.update(dico)</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id271626869419262">myDict['D'] = 4</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id681626869419637">print(myDict) # {'A': 1, 'B': 2, 'C': 3, 'D': 4}</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id801626869419846">propval = myDict.ConvertToPropertyValues()</paragraph>
</pycode>
<paragraph role="paragraph" id="par_id981626869718081">It is possible to create an instance of the <literal>Dictionary</literal> service using a Python <literal>dict</literal> object as argument as shown in the following example.</paragraph>
<pycode>
<paragraph role="pycode" localize="false" id="pyc_id891626869184970">dico = dict('A' = 1, 'B' = 2, 'C' = 3)</paragraph>
<paragraph role="pycode" id="pyc_id201626869185236"># Initialize myDict with the content of dico</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id201626869182054">myDict = CreateScriptService('Dictionary', dico)</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id831626869185400">myDict['D'] = 4</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id351626869185567">print(myDict) # {'A': 1, 'B': 2, 'C': 3, 'D': 4}</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id721626869185728">propval = myDict.ConvertToPropertyValues()</paragraph>
</pycode>
<note id="par_id211626699007613">Because Python has built-in dictionary support, most of the methods in the <literal>Dictionary</literal> service are available for Basic scripts only. Exceptions are <literal>ConvertToPropertyValues</literal> and <literal>ImportFromPropertyValues</literal> that are supported in both Basic and Python.</note>
<h2 id="hd_id351582885195476" xml-lang="en-US">Properties</h2>
<section id="properties_toc">
<table id="tab_id971582885195582">
<tablerow>
<tablecell>
<paragraph id="par_id41582885195836" role="tablehead" xml-lang="en-US">Name</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id31582885195372" role="tablehead" xml-lang="en-US">Read-only</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id31582885195238" role="tablehead" xml-lang="en-US">Type</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id931582885195131" role="tablehead" xml-lang="en-US" >Description</paragraph>
</tablecell>
</tablerow>
<tablerow>
<tablecell>
<paragraph id="par_id97158288519551" role="tablecontent" localize="false">Count</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id221582885195686" role="tablecontent" xml-lang="en-US">Yes</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id371582885195525" role="tablecontent" localize="false">Long</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id881582885195976" role="tablecontent" xml-lang="en-US">The number of entries in the dictionary</paragraph>
</tablecell>
</tablerow>
<tablerow>
<tablecell>
<paragraph id="par_id841582886030384" role="tablecontent" localize="false">Items</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id441582886030118" role="tablecontent" xml-lang="en-US">Yes</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id131582886030297" role="tablecontent" xml-lang="en-US">Array of Variants</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id471582886030489" role="tablecontent" xml-lang="en-US">The list of items as a one dimensional array</paragraph>
</tablecell>
</tablerow>
<tablerow>
<tablecell>
<paragraph id="par_id231582886791351" role="tablecontent" localize="false">Keys</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id971582886791838" role="tablecontent" xml-lang="en-US">Yes</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id271582886791111" role="tablecontent" xml-lang="en-US">Array of Strings</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id16158288679167" role="tablecontent" xml-lang="en-US">The list of keys as a one dimensional array</paragraph>
</tablecell>
</tablerow>
</table>
</section>
<paragraph role="tip" id="par_id461582886731495" xml-lang="en-US">The <literal>Keys</literal> and <literal>Items</literal> properties return their respective contents, using an identical ordering. The order is unrelated to the creation sequence.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<paragraph role="paragraph" id="par_id931610389113917">The following example uses the <literal>Keys</literal> property to iterate over all keys in the dictionary <literal>myDict</literal>.</paragraph>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id211582887012398">Dim a As Variant, b As String</paragraph>
<paragraph role="bascode" localize="false" id="bas_id351582887179042">a = myDict.Keys</paragraph>
<paragraph role="bascode" localize="false" id="bas_id251582887187534">For Each b In a</paragraph>
<paragraph role="bascode" localize="false" id="bas_id121582887198799"> MsgBox(myDict.Item(b))</paragraph>
<paragraph role="bascode" localize="false" id="bas_id571606473148931">Next b</paragraph>
</bascode>
<section id="methods_toc">
<table id="tab_id891606472825856">
<tablerow>
<tablecell colspan="3">
<paragraph id="par_id891611613601554" role="tablehead" xml-lang="en-US">List of Methods in the Dictionary Service</paragraph>
</tablecell>
</tablerow>
<tablerow>
<tablecell>
<paragraph id="par_id381606472825856" role="tablecontent" localize="false">
<link href="text/sbasic/shared/03/sf_dictionary.xhp#Add">Add</link><br/>
<link href="text/sbasic/shared/03/sf_dictionary.xhp#ConvertToArray">ConvertToArray</link><br/>
<link href="text/sbasic/shared/03/sf_dictionary.xhp#ConvertToJson">ConvertToJson</link><br/>
<link href="text/sbasic/shared/03/sf_dictionary.xhp#ConvertToPropertyValues">ConvertToPropertyValues</link>
</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id451606472825856" role="tablecontent" localize="false">
<link href="text/sbasic/shared/03/sf_dictionary.xhp#Exists">Exists</link><br/>
<link href="text/sbasic/shared/03/sf_dictionary.xhp#ImportFromJson">ImportFromJson</link><br/>
<link href="text/sbasic/shared/03/sf_dictionary.xhp#ImportFromPropertyValues">ImportFromPropertyValues</link><br/>
<link href="text/sbasic/shared/03/sf_dictionary.xhp#Item">Item</link>
</paragraph>
</tablecell>
<tablecell>
<paragraph id="par_id161606472825856" role="tablecontent" localize="false">
<link href="text/sbasic/shared/03/sf_dictionary.xhp#Remove">Remove</link><br/>
<link href="text/sbasic/shared/03/sf_dictionary.xhp#RemoveAll">RemoveAll</link><br/>
<link href="text/sbasic/shared/03/sf_dictionary.xhp#ReplaceItem">ReplaceItem</link><br/>
<link href="text/sbasic/shared/03/sf_dictionary.xhp#ReplaceKey">ReplaceKey</link>
</paragraph>
</tablecell>
</tablerow>
</table>
</section>
<section id="Add">
<comment> Add ---------------------------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id781582887670029">
<bookmark_value>Dictionary service;Add</bookmark_value>
</bookmark>
<h2 id="hd_id831582887670029" localize="false">Add</h2>
<paragraph role="paragraph" id="par_id831582887670029">Adds a new key-item pair into the dictionary. Returns <literal>True</literal> if successful.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id881626699610355">
<input>dict.Add(key: str, item: any): bool</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph role="paragraph" id="par_id341582887670030"><emph>key</emph>: String value used to identify the Item. The key is not case-sensitive.</paragraph>
<paragraph role="paragraph" id="par_id401582887670030"><emph>item</emph>: Any value, including an array, a Basic object, a UNO object, a dictionary, etc.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id191582887670030">Dim NewValue As Variant</paragraph>
<paragraph role="bascode" localize="false" id="bas_id391582887670030">myDict.Add("NewKey", NewValue)</paragraph>
</bascode>
<warning id="par_id1001610391308765">Every key must be unique in the same dictionary. If the key already exists in the dictionary, a <literal>DUPLICATEKEYERROR</literal> will be raised. Keys that are made of space characters will raise a <literal>INVALIDKEYERROR</literal> error.</warning>
</section>
<section id="ConvertToArray">
<comment> ConvertToArray ----------------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id621582888424103">
<bookmark_value>Dictionary service;ConvertToArray</bookmark_value>
</bookmark>
<h2 id="hd_id211582888424104" localize="false">ConvertToArray</h2>
<paragraph role="paragraph" id="par_id81582888424104">Stores the contents of the dictionary in a two-columns zero-based array. The keys are stored in the first column and the items are stored in the second column.</paragraph>
<paragraph role="paragraph" id="par_id341610391565678">If the dictionary is empty, this method will return an empty <literal>Array</literal>.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id71626699963594">
<input>dict.ConvertToArray(): any[0..*, 0..1]</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id651610391840491">Dim myDict as Variant</paragraph>
<paragraph role="bascode" localize="false" id="bas_id381610391862148">myDict = CreateScriptService("Dictionary")</paragraph>
<paragraph role="bascode" localize="false" id="bas_id61610391873262">myDict.Add("a", 1)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id351610391876967">myDict.Add("b", 2)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id491610391880959">myDict.Add("c", 3)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id521610391883264">Dim arr as Variant</paragraph>
<paragraph role="bascode" localize="false" id="bas_id361610391885584">arr = myDict.ConvertToArray()</paragraph>
<paragraph role="bascode" localize="false" id="bas_id911610391888240">'(("a", 1), ("b", 2), ("c", 3))</paragraph>
</bascode>
</section>
<section id="ConvertToJson">
<comment> ConvertToJson ------------------------------------------------------------------------------------------ </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id631601296836318">
<bookmark_value>Dictionary service;ConvertToJson</bookmark_value>
</bookmark>
<h2 id="hd_id851601296836224" localize="false">ConvertToJson</h2>
<paragraph role="paragraph" id="par_id831601296836981">Converts the contents of a dictionary to <link href="https://en.wikipedia.org/wiki/JSON">JSON (JavaScript Object Notation)</link> text.</paragraph>
<h3 id="hd_id261601297024828" xml-lang="en-US">Limitations</h3>
<paragraph role="paragraph" id="par_id401601297178073" xml-lang="en-US">This method supports the following data types: <literal>String</literal>, <literal>Boolean</literal>, numbers, <literal>Null</literal> and <literal>Empty</literal>. Arrays containing items of those types are also allowed, whatever their dimensions. Dates are converted into strings, however they cannot be used inside Arrays. Other data types are converted to their string representation using the <literal>SF_String.Represent</literal> service.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id901626700139467">
<input>dict.ConvertToJson(indent: str = ""): str</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph role="paragraph" id="par_id8816012968362"><emph>indent</emph>: When <literal>indent</literal> is a positive number or a text, JSON array elements and object members are pretty-printed with that indentation level. A negative <literal>indent</literal> value will add new lines with no indentation. The default value is an empty string "" which selects the most compact representation. Using a positive integer for <literal>indent</literal> indents that many spaces per level. When <literal>indent</literal> is a string, such as <literal>Chr(9)</literal> or <literal>Tab(1)</literal>, the Tab character is used to indent each level.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id671601298281962">myDict.Add("p0", 12.5)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id851601298288784">myDict.Add("p1", "a string àé""ê")</paragraph>
<paragraph role="bascode" localize="false" id="bas_id201601298295976">myDict.Add("p2", DateSerial(2020,9,28))</paragraph>
<paragraph role="bascode" localize="false" id="bas_id951601298323286">myDict.Add("p3", True)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id731601298329491">myDict.Add("p4", Array(1,2,3))</paragraph>
<paragraph role="bascode" localize="false" id="bas_id451601298336500">MsgBox myDict.ConvertToJson() </paragraph>
<paragraph role="bascode" localize="false" id="bas_id511610392557702">'{"p0": 12.5, "p1": "a string \u00e0\u00e9\"\u00ea", "p2": "2020-09-28", "p3": true, "p4": [1, 2, 3]}</paragraph>
</bascode>
</section>
<section id="ConvertToPropertyValues">
<comment> ConvertToPropertyValues --------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id991582889470402">
<bookmark_value>Dictionary service;ConvertToPropertyValues</bookmark_value>
<bookmark_value>API;PropertyValue</bookmark_value>
<bookmark_value>API;DateTime</bookmark_value>
</bookmark>
<h2 id="hd_id421582889470909" localize="false">ConvertToPropertyValues</h2>
<paragraph role="paragraph" id="par_id151582889470596">Stores the contents of the dictionary into an array of <literal>PropertyValues</literal>.</paragraph>
<paragraph role="paragraph" id="par_id231610392665049">Each entry in the array is a <literal>com.sun.star.beans.PropertyValue</literal>. The key is stored in <literal>Name</literal>, the item is stored in <literal>Value</literal>.</paragraph>
<paragraph role="paragraph" id="par_id341610392705367">If one of the items has a type <literal>Date</literal>, it is converted to a <literal>com.sun.star.util.DateTime</literal> structure. If one of the items is an empty array, it is converted to <literal>Null</literal>. The resulting array is empty when the dictionary is empty.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id481626700464253">
<input>dict.ConvertToPropertyValues(): obj[0..*]</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<embed href="text/sbasic/shared/00000003.xhp#In_Basic"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id81610393124521">Dim myDict as Variant</paragraph>
<paragraph role="bascode" localize="false" id="bas_id971610393127816">myDict = CreateScriptService("Dictionary")</paragraph>
<paragraph role="bascode" id="bas_id531610393130289">'Adds some properties to the dictionary</paragraph>
<paragraph role="bascode" localize="false" id="bas_id711610393132877">myDict.Add("Color", "Blue")</paragraph>
<paragraph role="bascode" localize="false" id="bas_id681610393135354">myDict.Add("Width", 20)</paragraph>
<paragraph role="bascode" id="bas_id571610393137959">'Converts to an Array of PropertyValue objects</paragraph>
<paragraph role="bascode" localize="false" id="bas_id351610393140357">Dim prop as Variant</paragraph>
<paragraph role="bascode" localize="false" id="bas_id101610393142717">prop = myDict.ConvertToPropertyValues()</paragraph>
</bascode>
<embed href="text/sbasic/shared/00000003.xhp#In_Python"/>
<paragraph role="paragraph" id="par_id771626700938786">Note in the example below that a Python dictionary needs to be passed as the second argument of the <literal>CreateScriptService</literal> method.</paragraph>
<pycode>
<paragraph role="pycode" localize="false" id="pyc_id691626700585141">myDict = dict()</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id811626700586188">myDict["Color"] = "Blue"</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id331626700586409">myDict["Width"] = 30</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id171626700586634">sfDic = CreateScriptService("Dictionary", myDict)</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id251626700586818">prop = sfDic.ConvertToPropertyValues()</paragraph>
</pycode>
<tip id="par_id421610393306916">Many services and methods in the UNO library take in parameters represented using the <literal>PropertyValue</literal> struct, which is part of the %PRODUCTNAME API.</tip>
</section>
<section id="Exists">
<comment> Exists ------------------------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id81582889812915">
<bookmark_value>Dictionary service;Exists</bookmark_value>
</bookmark>
<h2 id="hd_id461582889812915" localize="false">Exists</h2>
<paragraph role="paragraph" id="par_id841582889812916">Determines if a key exists in the dictionary.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id441626701103153">
<input>dict.Exists(key: str): bool</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph role="paragraph" id="par_id971582889812917"><emph>key</emph>: The key to be looked up in the dictionary.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id741582889812919">Dim myDict as Variant</paragraph>
<paragraph role="bascode" localize="false" id="bas_id471582889812919">myDict = CreateScriptService("Dictionary")</paragraph>
<paragraph role="bascode" id="bas_id811606485130666">'Adds some properties to the dictionary</paragraph>
<paragraph role="bascode" localize="false" id="bas_id391610393640781">myDict.Add("Color", "Blue")</paragraph>
<paragraph role="bascode" localize="false" id="bas_id891610393643755">myDict.Add("Width", 20)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id531610393646186">'(...)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id491610393648822">If Not myDict.Exists("Size") Then</paragraph>
<paragraph role="bascode" localize="false" id="bas_id681610393651551"> MsgBox "You have to provide a Size value"</paragraph>
<paragraph role="bascode" localize="false" id="bas_id581610393654335">End If</paragraph>
</bascode>
</section>
<section id="ImportFromJson">
<comment> ImportFromJson ----------------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id661601391980619">
<bookmark_value>Dictionary service;ImportFromJson</bookmark_value>
</bookmark>
<h2 id="hd_id81601391980848" localize="false">ImportFromJson</h2>
<paragraph role="paragraph" id="par_id791601391980978">Adds the content of a <link href="https://en.wikipedia.org/wiki/JSON">JSON (JavaScript Object Notation)</link> string into the current dictionary. Returns <literal>True</literal> if successful.</paragraph>
<h3 id="hd_id961601392113644" xml-lang="en-US">Limitations</h3>
<paragraph role="paragraph" id="par_id481601392181131" xml-lang="en-US">The JSON string may contain numbers, text, booleans, null values and arrays containing those types. It must not contain JSON objects namely sub-dictionaries.</paragraph>
<paragraph role="paragraph" id="par_id511601392205908" xml-lang="en-US">An attempt is made to convert text to date if the item matches one of these patterns: YYYY-MM-DD, HH:MM:SS or YYYY-MM-DD HH:MM:SS.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id841626701235169">
<input>dict.ImportFromJson(inputstr: str, overwrite: bool = False): bool</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph role="paragraph" id="par_id69160139198061"><emph>inputstr</emph>: The string to import.</paragraph>
<paragraph role="paragraph" id="par_id201601391980268"><emph>overwrite</emph>: When <literal>True</literal>, entries with same name may exist in the dictionary and their values are overwritten. When <literal>False</literal> (default), repeated keys will raise an error. Be aware that dictionary keys are not case-sensitive while names are case-sensitive in JSON strings.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id411601393205207">Dim s As String</paragraph>
<paragraph role="bascode" localize="false" id="bas_id891601393212562">s = "{'firstName': 'John','lastName': 'Smith','isAlive': true,'age': 66, 'birth': '1954-09-28 20:15:00'" _</paragraph>
<paragraph role="bascode" localize="false" id="bas_id221601393220997"> & ",'address': {'streetAddress': '21 2nd Street','city': 'New York','state': 'NY','postalCode': '10021-3100'}" _</paragraph>
<paragraph role="bascode" localize="false" id="bas_id931601393226582"> & ",'phoneNumbers': [{'type': 'home','number': '212 555-1234'},{'type': 'office','number': '646 555-4567'}]" _</paragraph>
<paragraph role="bascode" localize="false" id="bas_id671601393233042"> & ",'children': ['Q','M','G','T'],'spouse': null}"</paragraph>
<paragraph role="bascode" localize="false" id="bas_id131601393240390">s = Replace(s, "'", """")</paragraph>
<paragraph role="bascode" localize="false" id="bas_id351601393248554">myDict.ImportFromJson(s, OverWrite := True)</paragraph>
<paragraph role="bascode" id="bas_id521601393254694">'The (sub)-dictionaries "address" and "phoneNumbers" (0) and (1) are imported as Empty values.</paragraph>
</bascode>
</section>
<section id="ImportFromPropertyValues">
<comment> ImportFromPropertyValues ------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id791588941968651">
<bookmark_value>Dictionary service;ImportFromPropertyValues</bookmark_value>
</bookmark>
<h2 id="hd_id71588941968955" localize="false">ImportFromPropertyValues</h2>
<paragraph role="paragraph" id="par_id651588941968228">Inserts the contents of an array of <literal>PropertyValue</literal> objects into the current dictionary. <literal>PropertyValue</literal> Names are used as Keys in the dictionary, whereas Values contain the corresponding values. Returns <literal>True</literal> if successful.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id431626701465848">
<input>dict.ImportFromPropertyValues(propertyvalues: obj[0..*], overwrite: bool = False): bool</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph role="paragraph" id="par_id751588941968522"><emph>propertyvalues</emph>: A zero-based 1-dimensional array containing <literal>com.sun.star.beans.PropertyValue</literal> objects. This parameter may also be a single <literal>PropertyValue</literal> object not contained in an Array.</paragraph>
<paragraph role="paragraph" id="par_id21588941968131"><emph>overwrite</emph>: When <literal>True</literal>, entries with same name may exist in the dictionary and their values are overwritten. When <literal>False</literal> (default), repeated keys will raise an error. Note that dictionary keys are not case-sensitive in Basic, whereas names are case-sensitive in sets of property values and in Python dictionaries.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<paragraph role="paragraph" id="par_id641626703615898">The examples below first create an array with two <literal>PropertyValue</literal> objects and then convert it to a dictionary.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#In_Basic"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id951588941968987">Dim vProp As New com.sun.star.beans.PropertyValue</paragraph>
<paragraph role="bascode" localize="false" id="bas_id771588941968883">Dim arrProp : arrProp = Array()</paragraph>
<paragraph role="bascode" localize="false" id="bas_id831588943409828">vProp.Name = "Color"</paragraph>
<paragraph role="bascode" localize="false" id="bas_id891610395011237">vProp.Value = "Blue"</paragraph>
<paragraph role="bascode" localize="false" id="bas_id371626703397791">arrProp = SF_Array.Append(arrProp, vProp)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id71626703398189">vProp.Name = "Date"</paragraph>
<paragraph role="bascode" localize="false" id="bas_id1001626703398431">vProp.Value = CDateToUnoDateTime(Now)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id881626703398656">arrProp = SF_Array.Append(arrProp, vProp)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id761626703398879">myDict = CreateScriptService("Dictionary")</paragraph>
<paragraph role="bascode" localize="false" id="bas_id181626703399098">myDict.ImportFromPropertyValues(arrProp, Overwrite := True)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id121626703516023">Dim keys : keys = myDict.Keys</paragraph>
<paragraph role="bascode" localize="false" id="bas_id971626703516446">For Each k In keys</paragraph>
<paragraph role="bascode" localize="false" id="bas_id811626703516686"> MsgBox k & " - " & myDict.Item(k)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id261626703516863">Next k</paragraph>
</bascode>
<embed href="text/sbasic/shared/00000003.xhp#In_Python"/>
<pycode>
<paragraph role="pycode" localize="false" id="pyc_id121626701725559">from scriptforge import CreateScriptService</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id611626704519415">from datetime import datetime</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id221626701725743">import uno</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id911626701725885">bas = CreateScriptService("Basic")</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id941626701726044">arrProp = list()</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id911626701726205">vProp = uno.createUnoStruct("com.sun.star.beans.PropertyValue")</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id751626701726717">vProp.Name = "Color"</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id841626704518283">vProp.Value = "Blue"</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id291626704518547">arrProp.append(vProp)</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id271626704518745">vProp = uno.createUnoStruct("com.sun.star.beans.PropertyValue")</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id691626704518975">vProp.Name = "Date"</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id691626704519192">vProp.Value = bas.CDateToUnoDateTime(datetime.now())</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id631626704519647">arrProp.append(vProp)</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id881626704519864">myDict = CreateScriptService("Dictionary")</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id61626704520096">myDict.ImportFromPropertyValues(arrProp, overwrite=True)</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id181626704520322">for k in myDict.keys():</paragraph>
<paragraph role="pycode" localize="false" id="pyc_id221626704799909"> bas.MsgBox("{} - {}".format(k, myDict[k]))</paragraph>
</pycode>
</section>
<section id="Item">
<comment> Item --------------------------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id741582980366762">
<bookmark_value>Dictionary service;Item</bookmark_value>
</bookmark>
<h2 id="hd_id301582098366573" localize="false">Item</h2>
<paragraph role="paragraph" id="par_id891582890366737">Retrieves an existing dictionary entry based on its key. Returns the value of the item if successful, otherwise returns <literal>Empty</literal>.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id501626704949678">
<input>dict.Item(key: str): any</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph role="paragraph" id="par_id551582890399669"><emph>key</emph>: Not case-sensitive. If it does not exist, <literal>Empty</literal> value is returned.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<paragraph role="paragraph" id="par_id181610395933967">The following example iterates over all keys in the dictionary and uses the <literal>Item</literal> method to access their values.</paragraph>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id371528890366702">Dim myDict as Variant, k as Variant, allKeys as Variant</paragraph>
<paragraph role="bascode" localize="false" id="bas_id661610395517565">myDict = CreateScriptService("Dictionary")</paragraph>
<paragraph role="bascode" localize="false" id="bas_id181610395523430">myDict.Add("key1", 100)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id621610395527679">myDict.Add("key2", 200)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id871610395530158">myDict.Add("key3", 300)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id891610395534807">allKeys = myDict.Keys</paragraph>
<paragraph role="bascode" localize="false" id="bas_id71610395537640">For Each k in allKeys</paragraph>
<paragraph role="bascode" localize="false" id="bas_id411610395540304"> MsgBox(myDict.Item(k))</paragraph>
<paragraph role="bascode" localize="false" id="bas_id931610395543522">Next k</paragraph>
</bascode>
</section>
<section id="Remove">
<comment> Remove ------------------------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id741582890366762">
<bookmark_value>Dictionary service;Remove</bookmark_value>
</bookmark>
<h2 id="hd_id301582890366573" localize="false">Remove</h2>
<paragraph role="paragraph" id="par_id891582890388737">Removes an existing dictionary entry based on its key. Returns <literal>True</literal> if successful.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id861626705083007">
<input>dict.Remove(key: str): bool</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph role="paragraph" id="par_id551582890366999"><emph>key</emph>: Not case-sensitive. Must exist in the dictionary, otherwise an <literal>UNKNOWNKEYERROR</literal> error is raised.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id56158289627581">myDict.Add("key1", 100)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id651610399290742">myDict.Add("key2", 200)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id741610399296091">myDict.Add("key3", 300)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id591610399299316">MsgBox(myDict.Count) ' 3</paragraph>
<paragraph role="bascode" localize="false" id="bas_id661610399302807">myDict.Remove("key2")</paragraph>
<paragraph role="bascode" localize="false" id="bas_id671610399305808">MsgBox(myDict.Count) ' 2</paragraph>
</bascode>
</section>
<section id="RemoveAll">
<comment> RemoveAll ---------------------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id471582896275914">
<bookmark_value>Dictionary service;RemoveAll</bookmark_value>
</bookmark>
<h2 id="hd_id911582896275637" localize="false">RemoveAll</h2>
<paragraph role="paragraph" id="par_id921582896275624">Removes all the entries from the dictionary. Returns <literal>True</literal> if successful.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id561626705190520">
<input>dict.RemoveAll(): bool</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id56158289627580">myDict.Add("key1", 100)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id651610399290743">myDict.Add("key2", 200)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id741610399296092">myDict.Add("key3", 300)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id591610399299317">MsgBox(myDict.Count) ' 3</paragraph>
<paragraph role="bascode" localize="false" id="bas_id661610399302808">myDict.RemoveAll()</paragraph>
<paragraph role="bascode" localize="false" id="bas_id671610399305809">MsgBox(myDict.Count) ' 0</paragraph>
</bascode>
</section>
<section id="ReplaceItem">
<comment> ReplaceItem -------------------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id891582895615851">
<bookmark_value>Dictionary service;ReplaceItem</bookmark_value>
</bookmark>
<h2 id="hd_id801582895615477" localize="false">ReplaceItem</h2>
<paragraph role="paragraph" id="par_id281582895615444">Replaces an existing item value based on its key. Returns <literal>True</literal> if successful.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id551626705258936">
<input>dict.ReplaceItem(key: str, value: any): bool</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph role="paragraph" id="par_id991582895615535"><emph>key</emph>: String value representing the key whose value will be replaced. Not case-sensitive. If the key does not exist in the dictionary, an <literal>UNKNOWNKEYERROR</literal> error is raised.</paragraph>
<paragraph role="paragraph" id="par_id721582895615186"><emph>value</emph>: The new value of the item referred to with the <literal>key</literal> parameter.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id581582895615680">myDict.Add("a", 1)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id351582895615555">MsgBox(myDict.Item("a")) ' 1</paragraph>
<paragraph role="bascode" localize="false" id="bas_id361610399892402">myDict.ReplaceItem("a", 100)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id411610399896001">MsgBox(myDict.Item("a")) ' 100</paragraph>
</bascode>
</section>
<section id="ReplaceKey">
<comment> ReplaceKey --------------------------------------------------------------------------------------------- </comment>
<bookmark xml-lang="en-US" localize="false" branch="index" id="bm_id411582896597981">
<bookmark_value>Dictionary service;ReplaceKey</bookmark_value>
</bookmark>
<h2 id="hd_id231582896597839" localize="false">ReplaceKey</h2>
<paragraph role="paragraph" id="par_id571582896597766">Replaces an existing key in the dictionary by a new key. The item value is left unchanged. Returns <literal>True</literal> if successful.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functsyntax"/>
<paragraph role="paragraph" localize="false" id="par_id851626705393127">
<input>dict.ReplaceKey(key: str, value: str): bool</input>
</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functparameters"/>
<paragraph role="paragraph" id="par_id911582896597619"><emph>key</emph>: String value representing the key to be replaced. Not case-sensitive. If the key does not exist in the dictionary, a <literal>UNKNOWNKEYERROR</literal> error is raised.</paragraph>
<paragraph role="paragraph" id="par_id531582896597989"><emph>value</emph>: String value for the new key. Not case-sensitive. If the new key already exists in the dictionary, an <literal>DUPLICATEKEYERROR</literal> error is raised.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph role="bascode" localize="false" id="bas_id521582896597313">myDict.Add("oldKey", 100)</paragraph>
<paragraph role="bascode" localize="false" id="bas_id961610400326541">MsgBox(myDict.Item("oldKey")) ' 100</paragraph>
<paragraph role="bascode" localize="false" id="bas_id631610400329115">myDict.ReplaceKey("oldKey", "newKey")</paragraph>
<paragraph role="bascode" localize="false" id="bas_id761610400331545">MsgBox(myDict.Item("newKey")) ' 100</paragraph>
</bascode>
</section>
<embed href="text/sbasic/shared/03/lib_ScriptForge.xhp#SF_InternalUse"/>
<section id="relatedtopics">
<embed href="text/sbasic/shared/03/sf_array.xhp#ArrayService"/>
<embed href="text/sbasic/shared/collection.xhp#Collection_h1"/>
</section>
</body>
</helpdocument>
|