Category: Uncategorized

  • List all sheet names in Google Sheets via Google Apps Script

    This Google Apps Script lists all sheet names from the Google Sheet that you currently have open. It starts listing them in the currently selected cell, then moves down and lists one sheet per cell. It also links that cell with the corresponding sheet. -SM, 2024-08-15

    /**
     * This function prints the names of all sheets.
     * -SpreadsheetsMade, 2024-08-15
     */
    function printLinkedSheetNamesInAndBelowSelectedCell() {
    
      var ss;
      var sheets;
    
      var activeSheet;
      var printSheetNamesToThisColumn;
      var printSheetNamesToThisRow;
      var selectedCell;
    
      var currentSheet;
      var currentSheetName;
      var currentSheetURL;
      var richText;
    
      var i;
    
    
    
      ss = SpreadsheetApp.getActiveSpreadsheet();
    
      sheets = ss.getSheets();
    
      activeSheet = ss.getActiveSheet();
    
      selectedCell = activeSheet.getActiveCell();
    
      printSheetNamesToThisRow = selectedCell.getRow();
    
      printSheetNamesToThisColumn = selectedCell.getColumn();
    
    
    
      for (i = 0; i < sheets.length; i++) {
    
        currentSheet = sheets[i];
    
        currentSheetName = currentSheet.getSheetName();
    
        currentSheetURL = "#gid=" + currentSheet.getSheetId();
    
        // Create link to sheet
        richText = SpreadsheetApp.newRichTextValue()
          .setText(currentSheetName)
          .setLinkUrl(currentSheetURL)
          .build();
    
        // Print sheet names
        activeSheet.getRange(printSheetNamesToThisRow + i, printSheetNamesToThisColumn).setRichTextValue(richText);
      }
    }
  • Convert column letter to number with Apps Script

    This is an Apps Script function that converts column letter to column number. It is to be used with Google Sheets and works up to column ZZZ. -SM, 2026-05-10

    /**
     * Function converts column letter to column number
     * 
     * - @param columnLetter is required.
     * - @param startAt0or1 is optional.
     * 
     * - If @param startAt0or1 is 1,
     *   letter A is converted to number 1.
     * - If @param startAt0or1 is anything other than 1,
     *   letter A is converted to number 0.
     * 
     * - Google Sheets column limit is ZZZ
     *   (https://support.google.com/drive/answer/37603).
     */
    
    function columnLetterToNumber(columnLetter, startAt0or1) {
    
      const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      const alphabetLength = alphabet.length; // 26
      const colLetLen = columnLetter.length;
    
      const rightLetter = columnLetter.slice(colLetLen - 1, colLetLen);
    
      // Convert right letter to number
      let columnNumber = alphabet.indexOf(rightLetter);
    
      // If column letter is AA or bigger
      if (colLetLen > 1) {
        const middleLetter = columnLetter.slice(colLetLen - 2, colLetLen - 1);
    
        // Convert middle letter to number, and add it to existing column number
        columnNumber = columnNumber
          + (alphabet.indexOf(middleLetter) + 1)
          * alphabetLength;
    
    
        // If column letter is AAA or bigger
        if (colLetLen > 2) {
          const leftLetter = columnLetter.slice(colLetLen - 3, colLetLen - 2);
    
          // Convert left letter to number, and add it to existing column number
          columnNumber = columnNumber
            + (alphabet.indexOf(leftLetter) + 1)
            * alphabetLength
            * alphabetLength;
        }
      }
    
      // Define if column numbers start at 0 or 1 
      if (startAt0or1 === 1) {
        columnNumber++;
      }
    
      // Return column number
      return columnNumber;
    }
    

    And here is a short version of this function, but it works only up to and including column Z.

    function columnLetterToNumber(columnLetter) {
      return "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(columnLetter);
    }

    Old version of this function. Am leaving it here just for reference. -SM, 2024-07-28

    /**
     * Function converts column letter to column number
     * 
     * - @param columnLetter is required.
     * - @param startAt0or1 is optional.
     * 
     * - If @param startAt0or1 is 1,
     *   letter A is converted to number 1.
     * - If @param startAt0or1 is anything other than 1,
     *   letter A is converted to number 0.
     * 
     * - Google Sheets column limit is ZZZ
     *   (https://support.google.com/drive/answer/37603).
     */
    
    function columnLetterToNumber(columnLetter, startAt0or1) {
    
      /**
       * Declare all variables
       */
    
      var alphabet;
      var alphabetLength;
    
      var columnNumber;
    
      var letterLength;
      var letterLeft;
      var letterMiddle;
      var letterRight;
    
    
    
      /**
       * Initialize main variables
       */
    
      alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
      alphabetLength = alphabet.length; /*26*/
    
      letterLength = columnLetter.length;
    
    
    
      /**
       * Calculate column number
       */
    
      /* Get rightmost letter */
      letterRight = columnLetter.slice(letterLength - 1, letterLength);
    
      /* Convert rightmost letter to number */
      columnNumber = alphabet.indexOf(letterRight);
    
    
      if (letterLength > 1) {
    
        /* Get middle letter */
        letterMiddle = columnLetter.slice(letterLength - 2, letterLength - 1);
    
        /* Convert middle letter to number, and add it to existing column number */
        columnNumber = columnNumber + (alphabet.indexOf(letterMiddle) + 1) * alphabetLength;
    
    
        if (letterLength > 2) {
    
          /* Get leftmost letter */
          letterLeft = columnLetter.slice(letterLength - 3, letterLength - 2);
    
          /* Convert leftmost letter to number, and add it to existing column number */
          columnNumber = columnNumber + (alphabet.indexOf(letterLeft) + 1) * alphabetLength * alphabetLength;
        }
      }
    
    
    
      /**
       * Define if column numbers start at 0 or 1 
       */
    
      if (startAt0or1 == 1) {
    
        columnNumber++;
      }
    
    
    
      /**
       * Return column number
       */
    
      return columnNumber;
    }
  • Google Apps Script best practices

    Naming

    About naming conventions and capitalization.

    Constant names

    “JavaScript constants are also case-sensitive. However, these constants should be written in uppercase because they are nonchanging variables. If the variable declaration name contains more than one word, you should use UPPER_SNAKE_CASE.”
    syncfusion.com, 2024-02-26

    const EXAMPLE_CONSTANT_NAME = "Sweet";

    Function names

    “For function names, use camel case, starting with a lowercase character.”
    mozilla.org, 2024-02-24

    function exampleFunctionName() {
      ...
    }

    Other

    Things I have yet to categorize, but are important enough to list here.

    English

    Write code and comments in English instead of your first language. If you namely stop working on the project and people who inherited the project need to find a developer to change it, they will have many more to choose from as the vast majority of developers speak English. Not that many speak Slovene for example.
    —SM, 2024-08-17

    Single-line comments over multi-line comments

    While writing and testing Google Apps Script code I sometimes want to comment out an entire block of it. If I used multi-line comments all over the code, I will not able to do that as the multi-line comment will come to a close at the very first “*/” it encounters.

    I know many code editors give advance commenting and uncommenting options, but if you are writing Google Apps Script code inside Google’s native user interface, it is a lot easier to comment out bigger blocks of code by using single-line comments for almost everything and multi-line comments only for testing and development purposes.

    /**
     * This is how I used to write multi-line comments
     * using multi-line comment syntax
     */
    
    //
    // This is how I write multi-line comments now
    // using single-line comment syntax
    //

    —SM, 2024-08-17

  • Excel capitalize first letter

    How to capitalize first letter in Excel and Google Sheets. We will look at how to capitalize the first letter of every word (title case) and how to capitalize only the first letter of the first word (sentence case).

    Capitalize every word

    If you want title case where the first letter of every word is capitalized, and your text is in cell A1, use this formula:

    =PROPER(A1)
    Excel capitalize first letter 01 - Title case formula

    Capitalize only the very first letter

    If you want sentence case where only the first letter of the first word is capitalized, and your text is in cell A1, use this formula:

    =CONCATENATE(UPPER(LEFT(A1,1)),LOWER(RIGHT(A1,LEN(A1)-1)))
    Excel capitalize first letter - Sentence case formula

    This formula capitalizes only the very first letter and turns the rest into lowercase.

    If you want to capitalize the very first letter without turning the rest into lowercase, use the following formula instead:

    =CONCATENATE(UPPER(LEFT(A1,1)),RIGHT(A1,LEN(A1)-1))