<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2025-07-04T09:54:00+00:00</updated><id>/feed.xml</id><title type="html">Tolga’s &lt;codespace&gt;
&lt;/codespace&gt;</title><subtitle>@tbalci&apos;s personal space where he talks about the codes he is working on.</subtitle><author><name>Tolga Balcı</name></author><entry><title type="html">String Slicing in JavaScript and Python</title><link href="/javascript/python/2025/07/04/string-slicing-in-JavaScript-and-Python.html" rel="alternate" type="text/html" title="String Slicing in JavaScript and Python" /><published>2025-07-04T06:32:13+00:00</published><updated>2025-07-04T06:32:13+00:00</updated><id>/javascript/python/2025/07/04/string-slicing-in-JavaScript-and-Python</id><content type="html" xml:base="/javascript/python/2025/07/04/string-slicing-in-JavaScript-and-Python.html"><![CDATA[<p>I am posting this so that you don’t scratch your head with string slicing in JavaScript like me if you are coming from a Python background.</p>

<p>I spent some gray cells today while working on a capitalize function in JavaScript. The function simply takes a word as an input and capitalizes it.</p>

<p>I thought that since a string is an iterable, I could slice it with the indexes. So, I can get index 0, convert it to upper case, and then add <code>[1:]</code> as the remainder of the word.</p>

<p>Unfortunately that trick from Python did not work in JavaScript; Python’s <code>[start_index:end_index]</code> does not work. JavaScript only allows to access one index, not a range.</p>

<p>Therefore, the JavaScript method that we need to use is the <code>slice(start_index, end_index)</code>. But since JavaScript does not figure out the <code>end_index</code> automatically, we need to get it by the <code>string.length</code> method.</p>

<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">function</span> <span class="nx">capitalize</span><span class="p">(</span><span class="nx">word</span><span class="p">){</span>
<span class="k">return</span> <span class="nx">word</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toUpperCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">word</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nx">word</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span>
<span class="p">}</span></code></pre></figure>]]></content><author><name>Tolga Balcı</name></author><category term="JavaScript" /><category term="Python" /><summary type="html"><![CDATA[I am posting this so that you don’t scratch your head with string slicing in JavaScript like me if you are coming from a Python background. I spent some gray cells today while working on a capitalize function in JavaScript. The function simply takes a word as an input and capitalizes it. I thought that since a string is an iterable, I could slice it with the indexes. So, I can get index 0, convert it to upper case, and then add [1:] as the remainder of the word. Unfortunately that trick from Python did not work in JavaScript; Python’s [start_index:end_index] does not work. JavaScript only allows to access one index, not a range. Therefore, the JavaScript method that we need to use is the slice(start_index, end_index). But since JavaScript does not figure out the end_index automatically, we need to get it by the string.length method. function capitalize(word){ return word[0].toUpperCase() + word.slice(1, word.length); }]]></summary></entry><entry><title type="html">Loops in Python and JavaScript</title><link href="/javascript/python/2025/06/25/for-loops-in-JavaScript-and-Python.html" rel="alternate" type="text/html" title="Loops in Python and JavaScript" /><published>2025-06-25T13:24:12+00:00</published><updated>2025-06-25T13:24:12+00:00</updated><id>/javascript/python/2025/06/25/for-loops-in-JavaScript-and-Python</id><content type="html" xml:base="/javascript/python/2025/06/25/for-loops-in-JavaScript-and-Python.html"><![CDATA[<p>I’m juggling between JavaScript and Python (C for only Arduino) and sometimes I cannot wrap my head around the syntaxes, so I decided that it will be a good idea to create a table to refer to when I’m using different languages.</p>

<p>As a refresher,</p>
<ul>
  <li>do is an entry loop, which is the condition is evaluated and the loop is started if the condition is true.</li>
  <li>do-while is an exit loop; the loop is executed at least once, even if the condition never evaluates to true. Until the exit condition is met, the loop runs.</li>
</ul>

<p>Note here that Python does not have an explicit do-while construct. Therefore we “trick” Python by starting <code>while true</code>, which always evaluates to <code>true</code> and then specify the exit condition.</p>

<!--more-->

<table>
<tr>
<th>Loop</th>
<th>Python</th>
<th>JavaScript</th>
</tr>
<tr>
<td>for</td>
<td>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">for</span> <span class="p">(</span><span class="n">start_condition</span><span class="p">,</span> <span class="n">end_condition</span><span class="p">,</span> <span class="n">step</span><span class="p">):</span>
    <span class="n">statements</span></code></pre></figure>
</td>
<td>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="k">for</span> <span class="p">(</span><span class="nx">start_condition</span><span class="p">;</span> <span class="nx">end_condition</span><span class="p">;</span> <span class="nx">step</span><span class="p">)</span>
<span class="p">{</span><span class="nx">statements</span><span class="p">;}</span> </code></pre></figure>
</td>
</tr>
<tr>
<td>for in array<br />(elements only)</td>
<td>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">array</span><span class="p">:</span>
    <span class="n">statements</span></code></pre></figure>
</td>
<td>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">item</span> <span class="k">of</span> <span class="nx">array</span><span class="p">):</span>
<span class="p">{</span><span class="nx">statements</span><span class="p">;}</span> </code></pre></figure>
</td>
</tr>
<tr>
<td>for in array<br />(index and elements)</td>
<td>
<figure class="highlight"><pre><code class="language-python" data-lang="python"> <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">element</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">array</span><span class="p">):</span>
    <span class="n">statements</span></code></pre></figure>
</td>
<td>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"> <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">index</span> <span class="k">in</span> <span class="nx">array</span><span class="p">)</span>
<span class="p">{</span><span class="nx">statements</span> <span class="nx">array</span><span class="p">[</span><span class="nx">index</span><span class="p">];}</span> </code></pre></figure>
</td>
</tr>
<tr>
<td>for in objects</td>
<td>same for any iterable</td>
<td>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">index</span> <span class="k">of</span> <span class="nx">object</span><span class="p">)</span>
<span class="p">{</span><span class="nx">statements</span> <span class="nx">object</span><span class="p">[</span><span class="nx">index</span><span class="p">];}</span> </code></pre></figure>
</td>
</tr>
<tr>
<td>while</td>
<td>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">start_condition</span>
<span class="k">while</span> <span class="n">end_condition</span><span class="p">:</span>
    <span class="n">statements</span>
    <span class="n">iteration</span></code></pre></figure>

</td>
<td>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">let</span> <span class="nx">start_condition</span><span class="p">;</span>
<span class="k">while</span><span class="p">(</span><span class="nx">end_condition</span><span class="p">){</span>
<span class="nx">statements</span><span class="p">;</span>
<span class="nx">iteration</span><span class="p">;}</span></code></pre></figure>

</td>
</tr>
<tr>
<td>do while</td>
<td>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
  <span class="n">statements</span>
  <span class="n">iteration</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">end_condtion</span><span class="p">):</span>
    <span class="k">break</span></code></pre></figure>

</td>
<td>
<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">let</span> <span class="nx">start_condition</span><span class="p">;</span>
<span class="k">do</span><span class="p">{</span>
<span class="nx">statements</span><span class="p">;</span>
<span class="nx">iteration</span><span class="p">;}</span>
<span class="k">while</span> <span class="p">(</span><span class="nx">end_condition</span><span class="p">)</span></code></pre></figure>

</td>
</tr>
</table>]]></content><author><name>Tolga Balcı</name></author><category term="JavaScript" /><category term="Python" /><summary type="html"><![CDATA[I’m juggling between JavaScript and Python (C for only Arduino) and sometimes I cannot wrap my head around the syntaxes, so I decided that it will be a good idea to create a table to refer to when I’m using different languages. As a refresher, do is an entry loop, which is the condition is evaluated and the loop is started if the condition is true. do-while is an exit loop; the loop is executed at least once, even if the condition never evaluates to true. Until the exit condition is met, the loop runs. Note here that Python does not have an explicit do-while construct. Therefore we “trick” Python by starting while true, which always evaluates to true and then specify the exit condition.]]></summary></entry><entry><title type="html">JavaScript: Function Declarations vs Function Expressions</title><link href="/javascript/2025/06/03/JavaScript-Function-Declarations-vs-Function-Expressions.html" rel="alternate" type="text/html" title="JavaScript: Function Declarations vs Function Expressions" /><published>2025-06-03T12:53:18+00:00</published><updated>2025-06-03T12:53:18+00:00</updated><id>/javascript/2025/06/03/JavaScript-Function-Declarations-vs-Function-Expressions</id><content type="html" xml:base="/javascript/2025/06/03/JavaScript-Function-Declarations-vs-Function-Expressions.html"><![CDATA[<p>I am working on my JavaScript notes and will publish the ones that I think will benefit a larger audience.</p>

<p>This post is my notes on function declarations, function expressions and arrow functions and when to use each one.</p>

<!--more-->

<h2 id="the-core-difference">The Core Difference</h2>

<ul>
  <li><strong>Function Declaration</strong>: You’re telling JavaScript “here’s a function that exists”</li>
  <li><strong>Function Expression</strong>: You’re creating a function and storing it somewhere (usually in a variable)</li>
</ul>

<h2 id="basic-syntax-comparison">Basic Syntax Comparison</h2>

<table>
  <thead>
    <tr>
      <th style="text-align: left">Aspect</th>
      <th>Function Declaration</th>
      <th>Function Expression</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="text-align: left">Syntax</td>
      <td><code class="language-plaintext highlighter-rouge">function myFunction() { }</code></td>
      <td><code class="language-plaintext highlighter-rouge">const myFunction = function() { }</code></td>
    </tr>
    <tr>
      <td style="text-align: left">Keyword first</td>
      <td>Yes, starts with function</td>
      <td>No, starts with variable declaration</td>
    </tr>
    <tr>
      <td style="text-align: left">Name required</td>
      <td>Yes, must have a name</td>
      <td>No, can be anonymous</td>
    </tr>
    <tr>
      <td style="text-align: left">Semicolon</td>
      <td>No semicolon needed</td>
      <td>Semicolon needed (it’s a statement)</td>
    </tr>
  </tbody>
</table>

<h2 id="simple-examples">Simple Examples</h2>

<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// Function Declaration</span>
<span class="kd">function</span> <span class="nx">sayHelloDeclaration</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="dl">"</span><span class="s2">Hello from declaration!</span><span class="dl">"</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">// Function Expression</span>
<span class="kd">const</span> <span class="nx">sayHelloExpression</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="dl">"</span><span class="s2">Hello from expression!</span><span class="dl">"</span><span class="p">;</span>
<span class="p">};</span>

<span class="c1">// Both work the same way when called</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">sayHelloDeclaration</span><span class="p">());</span> <span class="c1">// "Hello from declaration!"</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">sayHelloExpression</span><span class="p">());</span>  <span class="c1">// "Hello from expression!"</span></code></pre></figure>

<h2 id="the-key-behavioral-difference-hoisting">The Key Behavioral Difference: Hoisting</h2>

<p>This is where they behave completely differently:</p>

<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// This works - you can call it BEFORE it's defined</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">callMeEarly</span><span class="p">());</span> <span class="c1">// "I was called early!"</span>

<span class="kd">function</span> <span class="nx">callMeEarly</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="dl">"</span><span class="s2">I was called early!</span><span class="dl">"</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">// This breaks - you CANNOT call it before it's defined</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">callMeLate</span><span class="p">());</span> <span class="c1">// ERROR: Cannot access 'callMeLate' before initialization</span>

<span class="kd">const</span> <span class="nx">callMeLate</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="dl">"</span><span class="s2">I was called late!</span><span class="dl">"</span><span class="p">;</span>
<span class="p">};</span></code></pre></figure>

<p><strong>Why this happens</strong>: JavaScript hoists function declarations to the top of their scope, but function expressions are <em>not</em> hoisted.</p>

<h2 id="when-to-use-each-one">When to Use Each One</h2>

<table>
  <thead>
    <tr>
      <th style="text-align: left">Situation</th>
      <th>Use This</th>
      <th>Why</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="text-align: left">General functions you’ll call throughout your code</td>
      <td>Function Declaration</td>
      <td>Hoisting makes them available everywhere in scope</td>
    </tr>
    <tr>
      <td style="text-align: left">Functions that depend on conditions or other variables</td>
      <td>Function Expression</td>
      <td>More predictable - can’t be called before creation</td>
    </tr>
    <tr>
      <td style="text-align: left">Functions you want to pass around as values</td>
      <td>Function Expression</td>
      <td>Clearer that it’s a value being assigned</td>
    </tr>
    <tr>
      <td style="text-align: left">Functions inside objects or arrays</td>
      <td>Function Expression</td>
      <td>Required syntax</td>
    </tr>
    <tr>
      <td style="text-align: left">Callback functions</td>
      <td>Function Expression</td>
      <td>More common pattern</td>
    </tr>
  </tbody>
</table>

<h2 id="practical-examples">Practical Examples</h2>

<h3 id="use-function-declaration-when">Use Function Declaration When:</h3>

<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// Main functions that other code depends on</span>
<span class="kd">function</span> <span class="nx">calculateTax</span><span class="p">(</span><span class="nx">amount</span><span class="p">,</span> <span class="nx">rate</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">amount</span> <span class="o">*</span> <span class="nx">rate</span><span class="p">;</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">formatCurrency</span><span class="p">(</span><span class="nx">amount</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="s2">`$</span><span class="p">${</span><span class="nx">amount</span><span class="p">.</span><span class="nx">toFixed</span><span class="p">(</span><span class="mi">2</span><span class="p">)}</span><span class="s2">`</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">// These can be called from anywhere in this scope</span>
<span class="kd">const</span> <span class="nx">total</span> <span class="o">=</span> <span class="nx">calculateTax</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mf">0.08</span><span class="p">);</span>
<span class="kd">const</span> <span class="nx">formatted</span> <span class="o">=</span> <span class="nx">formatCurrency</span><span class="p">(</span><span class="nx">total</span><span class="p">);</span></code></pre></figure>

<h3 id="use-function-expression-when">Use Function Expression When:</h3>

<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// Conditional function creation</span>
<span class="kd">let</span> <span class="nx">mathOperation</span><span class="p">;</span>

<span class="k">if</span> <span class="p">(</span><span class="nx">userWantsAddition</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">mathOperation</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">;</span>
    <span class="p">};</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="nx">mathOperation</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">a</span> <span class="o">*</span> <span class="nx">b</span><span class="p">;</span>
    <span class="p">};</span>
<span class="p">}</span>

<span class="c1">// Callback functions</span>
<span class="kd">const</span> <span class="nx">numbers</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">];</span>
<span class="kd">const</span> <span class="nx">doubled</span> <span class="o">=</span> <span class="nx">numbers</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">number</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">number</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span>
<span class="p">});</span>

<span class="c1">// Functions as object properties</span>
<span class="kd">const</span> <span class="nx">calculator</span> <span class="o">=</span> <span class="p">{</span>
    <span class="na">add</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">;</span>
    <span class="p">},</span>
    <span class="na">subtract</span><span class="p">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">a</span> <span class="o">-</span> <span class="nx">b</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">};</span></code></pre></figure>

<h2 id="arrow-functions-a-special-type-of-function-expression">Arrow Functions (A Special Type of Function Expression)</h2>

<p>Arrow functions are always function expressions:</p>

<figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// Traditional function expression</span>
<span class="kd">const</span> <span class="nx">addTraditional</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">;</span>
<span class="p">};</span>

<span class="c1">// Arrow function expression</span>
<span class="kd">const</span> <span class="nx">addArrow</span> <span class="o">=</span> <span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">;</span>
<span class="p">};</span>

<span class="c1">// Short arrow function (implicit return)</span>
<span class="kd">const</span> <span class="nx">addShort</span> <span class="o">=</span> <span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">;</span></code></pre></figure>

<h2 id="complete-comparison-table">Complete Comparison Table</h2>

<table>
  <thead>
    <tr>
      <th style="text-align: left">Feature</th>
      <th>Function Declaration</th>
      <th>Function Expression</th>
      <th>Arrow Function</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="text-align: left">Hoisting</td>
      <td>Yes, fully hoisted</td>
      <td>No, not hoisted</td>
      <td>No, not hoisted</td>
    </tr>
    <tr>
      <td style="text-align: left">Can be anonymous</td>
      <td>No, needs a name</td>
      <td>Yes</td>
      <td>Yes</td>
    </tr>
    <tr>
      <td style="text-align: left">Has own <code class="language-plaintext highlighter-rouge">this</code></td>
      <td>Yes</td>
      <td>Yes</td>
      <td>No, inherits <code class="language-plaintext highlighter-rouge">this</code></td>
    </tr>
    <tr>
      <td style="text-align: left">Can be constructors</td>
      <td>Yes</td>
      <td>Yes</td>
      <td>No</td>
    </tr>
    <tr>
      <td style="text-align: left">Block scope</td>
      <td>No, function scoped</td>
      <td>Yes, if using let/const</td>
      <td>Yes, if using let/const</td>
    </tr>
  </tbody>
</table>

<h2 id="when-each-is-required">When Each Is Required</h2>

<p><strong>Must use Function Expression for:</strong></p>

<ul>
  <li>Object methods (modern way)</li>
  <li>Array callbacks</li>
  <li>Conditional function creation</li>
  <li>IIFE (Immediately Invoked Function Expressions)</li>
</ul>

<p><strong>Must use Function Declaration for:</strong></p>

<ul>
  <li>When you need hoisting behavior</li>
  <li>Recursive functions that reference themselves by name</li>
  <li>When creating constructors (though classes are preferred now)</li>
</ul>

<p>The simple rule of thumb: Use function declarations for your main, standalone functions. Use function expressions for functions that are values, callbacks, or conditionally created.</p>]]></content><author><name>Tolga Balcı</name></author><category term="JavaScript" /><summary type="html"><![CDATA[My detailed notes on function declarations, function expressions and arrow functions, their comparison, and their use cases.]]></summary></entry><entry><title type="html">Office Scripts: Getting Values in Range</title><link href="/office-scripts/2025/05/30/Office-Scripts-Getting-Values-in-Range.html" rel="alternate" type="text/html" title="Office Scripts: Getting Values in Range" /><published>2025-05-30T06:48:38+00:00</published><updated>2025-05-30T06:48:38+00:00</updated><id>/office-scripts/2025/05/30/Office-Scripts-Getting-Values-in-Range</id><content type="html" xml:base="/office-scripts/2025/05/30/Office-Scripts-Getting-Values-in-Range.html"><![CDATA[<p>I’m working on an Office Script on Microsoft Excel and trying to understand what is happening.</p>

<p>I have an Excel workbook where I am trying to get the values in a range to an array. The range is 98 columns with varying number of rows, and contains duplicate values accross the cells. The range contains 2248 values, and I need to reduce them to unique values to further use in the code.</p>

<p>The steps are:</p>
<ol>
  <li>Get all the values in the range to an array,</li>
  <li>Make sure the array contains only the unique values.</li>
</ol>

<p><strong>Step 1</strong></p>

<p>The most efficient way to get the values in this range would be using the <code class="language-plaintext highlighter-rouge">getValues()</code> function on the range:
<!--more--></p>

<figure class="highlight"><pre><code class="language-typescript" data-lang="typescript"><span class="c1">// First row contains values, hence the offset range:</span>
<span class="nx">dataRange</span> <span class="o">=</span> <span class="nx">dataSheet</span><span class="p">.</span><span class="nx">getUsedRange</span><span class="p">().</span><span class="nx">getOffsetRange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
<span class="nx">allValuesArr</span> <span class="o">=</span> <span class="nx">dataRange</span><span class="p">.</span><span class="nx">getValues</span><span class="p">()</span></code></pre></figure>

<p>The problem that I had with this piece of code was,  whatever the number of rows and columns are present in the range, the array length was always 101. Checking the daataRange with <code class="language-plaintext highlighter-rouge">dataRange.getAddress()</code> always returned the correct address. In my case the range is A2:BF102.</p>

<p>The interesting this was, when I ran a for loop in the dataRange, I got the values correctly:</p>

<figure class="highlight"><pre><code class="language-typescript" data-lang="typescript"><span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">colCount</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">colCount</span> <span class="o">&lt;=</span> <span class="nx">dataSheetColumnCount</span><span class="p">;</span> <span class="nx">colCount</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">rowCount</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">rowCount</span> <span class="o">&lt;</span> <span class="nx">dataSheetRowCount</span><span class="p">;</span> <span class="nx">rowCount</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="kd">let</span> <span class="nx">cellValue</span> <span class="o">=</span> <span class="nx">dataRange</span><span class="p">.</span><span class="nx">getCell</span><span class="p">(</span><span class="nx">rowCount</span><span class="p">,</span> <span class="nx">colCount</span><span class="p">).</span><span class="nx">getValue</span><span class="p">()?.</span><span class="nx">toString</span><span class="p">();</span>
        <span class="nx">allValuesArr</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">cellValue</span><span class="p">)</span></code></pre></figure>

<p>I digged in and out of many debugging scenarios, trying to understand the discrepancy between the built-in getValues() function. Once I had run out of ideas, I hit Stack Overflow and picked <a href="https://stackoverflow.com/users/22192445/taller">Taller</a>’s brains (he was so kind, helpful and patient - see <a href="https://stackoverflow.com/questions/79618114/office-script-different-array-length-results-between-getvalues-and-looping">Stack Overflow post</a>.)</p>

<p>Nothing seemed to work, except one morning I realized that there could be an issue with the worksheet that the user provided, and which we are working on.</p>

<p>Boom! It was exactly what I thought. The user had inconsistent formatting in the cells, had some disjointed cells throughout the worksheet and various cell background fills. This was getting the script confused and it was only getting the first 101 values in the A44:E44 range. Once I cleared all the formatting and ran the <code class="language-plaintext highlighter-rouge">dataRange.getValues()</code> I got all the values on the sheet without any hiccups.</p>

<p><strong>Step 2</strong></p>

<p>The next step is to get the unique values. There are a lot of options/algorithms to achieve that, but for arrays my preference is to use the <code class="language-plaintext highlighter-rouge">Set</code> data type where I can. I find this to be way efficient than other methods, as the <code class="language-plaintext highlighter-rouge">Set</code> data type is a built-in data type in the languages that I use (Python, VBA, JavaScript, Office Scripts) and therefore already optimized.</p>

<p>The line below creates a new set from the allValuesArr, effectively removing all duplicates, and then converts it back to an array with the function <code class="language-plaintext highlighter-rouge">Array.from</code>.</p>

<figure class="highlight"><pre><code class="language-typescript" data-lang="typescript"><span class="c1">// Following the variable names from above</span>
<span class="nx">uniqueValuesArr</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="k">from</span><span class="p">(</span><span class="k">new</span> <span class="nb">Set</span><span class="p">(</span><span class="nx">allValuesArr</span><span class="p">))</span></code></pre></figure>

<p>The conversions are almost done on the fly, does not need any algorithms/code changes and simple enough to write/understand.</p>]]></content><author><name>Tolga Balcı</name></author><category term="Office-Scripts" /><summary type="html"><![CDATA[Office Scripts returns incorrect result with .getValues() in a specific scenario. I explain what has happened and how I solved it.]]></summary></entry><entry><title type="html">Jekyll install on WSL2 on Windows 11</title><link href="/jekyll/install/2025/05/22/Jekyll-install-on-WSL2.html" rel="alternate" type="text/html" title="Jekyll install on WSL2 on Windows 11" /><published>2025-05-22T18:48:38+00:00</published><updated>2025-05-22T18:48:38+00:00</updated><id>/jekyll/install/2025/05/22/Jekyll-install-on-WSL2</id><content type="html" xml:base="/jekyll/install/2025/05/22/Jekyll-install-on-WSL2.html"><![CDATA[<p>OK, I finally bit the bullet and decided to go with a static site generator.</p>

<p>I looked around on how to best go with Github Pages - Jekyll setup and saw that there were many references to setting Jekyll up on Linux, but I was thinking why wouldn’t I go with WSL2 on my Windows box?</p>

<p>I managed to do that and got Jekyll working. Here are the steps that I took:</p>

<!--more-->

<ol>
  <li>Clone your website repository</li>
</ol>

<figure class="highlight"><pre><code class="language-shell" data-lang="shell"><span class="nb">mkdir </span>website
<span class="nb">cd </span>website
git clone &lt;your-github-page-username.github.io&gt;</code></pre></figure>

<ol>
  <li>Update your .bashrc with the gem install locations.
<em>You should never run gems as root.</em> Following is from the <a href="https://jekyllrb.com/docs/installation/ubuntu/">official Jekyll documentation</a></li>
</ol>

<figure class="highlight"><pre><code class="language-shell" data-lang="shell"><span class="nb">echo</span> <span class="s1">'# Install Ruby Gems to ~/gems'</span> <span class="o">&gt;&gt;</span> ~/.bashrc
<span class="nb">echo</span> <span class="s1">'export GEM_HOME="$HOME/gems"'</span> <span class="o">&gt;&gt;</span> ~/.bashrc
<span class="nb">echo</span> <span class="s1">'export PATH="$HOME/gems/bin:$PATH"'</span> <span class="o">&gt;&gt;</span> ~/.bashrc</code></pre></figure>

<ol>
  <li>Install prerequisites and Ruby (See note below)</li>
</ol>

<figure class="highlight"><pre><code class="language-shell" data-lang="shell"><span class="nb">sudo </span>apt update <span class="o">&amp;&amp;</span> <span class="nb">sudo </span>apt upgrade <span class="nt">-y</span>
<span class="nb">sudo </span>apt <span class="nb">install </span>make gcc gpp build-essential zlib1g zlib1g-dev ruby-full </code></pre></figure>

<ol>
  <li>Install bundler and Jekyll</li>
</ol>

<figure class="highlight"><pre><code class="language-shell" data-lang="shell"><span class="nb">sudo </span>gem <span class="nb">install </span>bundler
<span class="nb">sudo </span>gem <span class="nb">install </span>jekyll 
jekyll <span class="nt">-v</span> <span class="c"># to check if Jekyll installed successfully.</span></code></pre></figure>

<p>If Jekyll is installed successfully, you should get the version number, e.g. <code class="language-plaintext highlighter-rouge">jekyll 4.4.1</code>.</p>

<p>If you run <code class="language-plaintext highlighter-rouge">gem install jekyll</code> (or <code class="language-plaintext highlighter-rouge">gem install jekyll bundler</code>) and  without sudo, you will get the following error:</p>

<figure class="highlight"><pre><code class="language-shell" data-lang="shell">ERROR:  While executing gem ... <span class="o">(</span>Gem::FilePermissionError<span class="o">)</span>
 You don<span class="s1">'t have write permissions for the /var/lib/gems/3.0.0 directory.</span></code></pre></figure>

<ol>
  <li>Switch to your local repository (per Step 1) - note the dot (.) at the end of the bundle exec command</li>
</ol>

<figure class="highlight"><pre><code class="language-shell" data-lang="shell"><span class="nb">cd </span>website/your-github-page-username.github.io
bundle <span class="nb">exec </span>jekyll new <span class="nt">--force</span> <span class="nt">--skip-bundle</span> <span class="nb">.</span>
bundle init
bundle add jekyll
bundle <span class="nb">install
</span>bundle info jekyll <span class="c"># to check if Jekyll installed successfully in our local repository</span></code></pre></figure>

<p>Again, if Jekyll website is built successfully, the output would be:</p>

<figure class="highlight"><pre><code class="language-shell" data-lang="shell"> <span class="k">*</span> jekyll <span class="o">(</span>4.4.1<span class="o">)</span>
 Summary: A simple, blog aware, static site generator.
 Homepage: https://jekyllrb.com
 Source Code: https://github.com/jekyll/jekyll
 Changelog: https://github.com/jekyll/jekyll/releases
 Bug Tracker: https://github.com/jekyll/jekyll/issues
 Path: /var/lib/gems/3.0.0/gems/jekyll-4.4.1
 Reverse Dependencies:
jekyll-feed <span class="o">(</span>0.17.0<span class="o">)</span> depends on jekyll <span class="o">(&gt;=</span> 3.7, &lt; 5.0<span class="o">)</span>
jekyll-seo-tag <span class="o">(</span>2.8.0<span class="o">)</span> depends on jekyll <span class="o">(&gt;=</span> 3.8, &lt; 5.0<span class="o">)</span>
minima <span class="o">(</span>2.5.2<span class="o">)</span> depends on jekyll <span class="o">(&gt;=</span> 3.5, &lt; 5.0<span class="o">)</span></code></pre></figure>

<ol>
  <li>Add theme per its documentation</li>
</ol>

<p>See supported themes: https://pages.github.com/themes/
<em>Play it safe now, and go with one of the themes in the link</em>.</p>

<ol>
  <li>Copy theme files under your local repository directory. Per Step 2, they are installed under ~/gems directory.</li>
</ol>

<figure class="highlight"><pre><code class="language-shell" data-lang="shell"><span class="nb">cd </span>website/your-github-page-username.github.io
bundle show &lt;your theme name&gt;
<span class="o">&gt;</span> /home/your-user-name/gems/gems/theme-name
<span class="nb">cp</span> <span class="nt">-r</span> /home/your-user-name/gems/gems/theme-name/<span class="k">*</span> .</code></pre></figure>

<ol>
  <li>I’d suggest to run <code class="language-plaintext highlighter-rouge">jekyll new --force .</code> at this point so that Jekyll installer generates tha necessary configuration files.</li>
</ol>

<figure class="highlight"><pre><code class="language-shell" data-lang="shell">jekyll new <span class="nt">--force</span> .</code></pre></figure>

<p>In this step, if you get an error like the following, delete one of the files. For example, delete about.markdown and index.markdown considering the following error:</p>

<figure class="highlight"><pre><code class="language-shell" data-lang="shell">Jekyll Feed: Generating feed <span class="k">for </span>posts
 Conflict: The following destination is shared by multiple files.
  The written file may end up with unexpected contents.
/mnt/c/Users/tolga/repos/websites/tbalci-github-io/_site/about/index.html
 - about.markdown
 - about.md

  Conflict: The following destination is shared by multiple files.
The written file may end up with unexpected contents.
/mnt/c/Users/tolga/repos/websites/tbalci-github-io/_site/index.html
 - index.markdown
 - index.md</code></pre></figure>

<p>Credits:</p>
<ol>
  <li><a href="https://stackoverflow.com/users/13877791/steven-whitaker">Stephen Whitaker</a> on his post on <a href="https://stackoverflow.com/questions/61704004/trouble-creating-a-new-jekyll-site-could-not-locate-gemfile-or-bundle-direct">StackOverflow</a></li>
  <li><a href="https://seankilleen.com/2020/07/building-my-jekyll-blog-with-ubuntu-on-wsl2/">Sean Killeen</a></li>
</ol>]]></content><author><name>Tolga Balcı</name></author><category term="jekyll" /><category term="install" /><summary type="html"><![CDATA[OK, I finally bit the bullet and decided to go with a static site generator. I looked around on how to best go with Github Pages - Jekyll setup and saw that there were many references to setting Jekyll up on Linux, but I was thinking why wouldn’t I go with WSL2 on my Windows box? I managed to do that and got Jekyll working. Here are the steps that I took:]]></summary></entry></feed>