Google Apps Script – Iterating Through Ranges in Sheets the Right and Wrong Way

Google Apps Script: SpreadsheetApp, getRange, getLastRow, getDataRange

I was trying to rush out some Google Apps Script code to deal with a task on Google Sheets recently. Basically, I had to search through a heap of data and find certain values and do something too them.

My column was reaching across the page to something like Column BK and my rows were over 1000 deep. Running this code was taking forever!!!

My immediate instinct was:

What have I done wrong?

…and my instinct was right.

The Good and Bad Way to Search Through Code

So after looking at my code again, I discovered that for some reason I go it into my head that I should be searching each cell for the value I needed and then doing something with it.

Sounds logical right? It’s sorta what you are meant to do.

The problem is that I was calling the sever and asking for the range in each cell as I was looping through the entire document. This is super costly and inefficient in terms of time.

Google talks about this in their Google Apps Script Best Practice page under Batch Operations.

Also, if you do run a costly code like this, then you will get a little red light in your Script tool bar that represents your Execution Hints:

Google Apps Script Execution Hints

Clicking on Execution Hints and expanding the side bar with provide you with a far-too-deserving-polite dressing down about your slow and server costly code.

Method Range.getValue is heavily used

The Good

Continue reading “Google Apps Script – Iterating Through Ranges in Sheets the Right and Wrong Way”

Copy and paste ranges in excel with OpenPyXl and Python 3

OpenPyXl is a Python open library that allows you to read and write Microsoft Excel files. Specifically, the ‘*.xlsx’ file extension. It helps you to create programs to create and modify files and automate your processes in excel.

Python Logo

NOTE: This post requires that you have some knowledge of Python and the OpenPyXl library. The library also needs to be installed for you to use. 

Quite often, I find that I have to work with ranges of data that I need to either copy and paste into a new file or files, or copy > modify > paste into files.

The OpenPyXl library allows you to look at every cell of a file and either copy it or modify it by using the openpyxl.worksheet.Worksheet.cell() method. This method allows you to access each cell by the row and column as a numerical value. 

Note! Unlike everything else in coding, rows and columns start with one(1) and not zero(0).

To select whole ranges of our data we need to iterate through it by both row and column and then store that data in a list to be pasted to the new file, spreadsheet or location that we desire.

The following example will take you through the process. For your own data you will need to modify the file, sheet and range locations. Everything else should be good to go.

You can find the whole code at the end of the post.

Why does your MS Excel look weird?

To be honest my screenshots of the ‘.xlsx files will be in Libreoffice. But this simple example will be able to load without issue in MS Excel.

The Example

Continue reading “Copy and paste ranges in excel with OpenPyXl and Python 3”

Spacer – Free Google Add-On That Spaces Rows by Column Category (Updated January 2022)

What is Spacer?

Spacer is a Free Google Add-On that creates row spaces by column categories.

I use it as an academic administrator to create a space or two at the end of each class section to create a visual break between sections. This makes it easier for my staff to quickly identify class sections.

You can find Spacer at the Google Workspace Marketplace.


Google Workspace Marketplace badge

What can Spacer do?

Spacer can:

  • Automatically detect the width of your columns.
  • Allow you to select a column with the categories you want to use to space your data.
  • Set the height of your header – so that is not spaced too.
  • Set the thickness of your spaces.
  • Set the color of your spaces
  • Set starting and end position of your color filler.

How to use Spacer

After Installation

Before You Start:

  1. Before you start, create a Google Sheet with the data in it you want to space.
  2. Select All the data.
  3. Go to the menu bar and select: Data>Sort Range...   and select the column you want to sort by. This will be the column you will use to space your data by the categories in that column.
Running Spacer:

Continue reading “Spacer – Free Google Add-On That Spaces Rows by Column Category (Updated January 2022)”

Can I modify Google Sheets with code? [updated January 2022]

Google Apps Script and the Google Suite

Updated January 2022

You sure can. Google has a fully supported script editor that you integrate with your Sheets, Docs, Forms, Slide, Gmail, Calendar and pretty much every aspect of the Google Suite. Its name: Google Apps Script.

Google Apps Script allows you to do all sorts of things like building short code to modify sheets and docs, create macros, develop add-ons mess around with Gmail and so much more.

Google Apps Scripts is based on Javascript. It can integrate with HTML5, CSS and Javascript well with its HTML service class.

Google has an extensive reference library to get you started.

Let’s get started with a basic example.

Dawh…Your first Google Script

Continue reading “Can I modify Google Sheets with code? [updated January 2022]”