How to Create a Google Apps Script Library: Reusable Code and Functions

How to Create a Google Apps Script Library: Reusable Code and Functions

Google Apps Script is a powerful tool for automating and extending the functionality of various Google Workspace applications. One of its hidden gems is the ability to create libraries—collections of reusable code and functions. In this blog post, we'll explore how to create a Google Apps Script library and harness the power of reusable code.

Why Use Google Apps Script Libraries?

Google Apps Script libraries offer several advantages for developers and automation enthusiasts:

  • Reusable Code: Create functions and scripts once and use them across multiple projects.
  • Efficient Collaboration: Share libraries with team members for consistent code and easy updates.
  • Error Reduction: Maintain code in one central location to minimize errors and debugging time.
  • Version Control: Keep track of library versions and updates for stability.

Creating a Google Apps Script Library

Creating a library is straightforward. Here's how you can get started:

  1. Open Google Apps Script and create a new project or open an existing one.
  2. Clear any default code in the script editor and replace it with the following code. This library provides functions to format a range of cells, calculate the sum of a column, and highlight cells with specific criteria.
  3.         // Library Code: Reusable Functions
           
            function formatRange(range, bgColor, textStyle) {
                const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
                const rangeObj = sheet.getRange(range);
                rangeObj.setBackground(bgColor);
                rangeObj.setFontWeight(textStyle === 'bold' ? 'bold' : 'normal');
                rangeObj.setFontStyle(textStyle === 'italic' ? 'italic' : 'normal');
              }
            
            function sumColumn(column) {
                const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
                const range = sheet.getRange(column + '1:' + column);
                const values = range.getValues();
                const total = values.reduce((sum, row) => sum + (parseFloat(row[0]) || 0), 0);
                Logger.log('Total sum of column ' + column + ': ' + total);
              }
    
    
                                            
  4. Click the Save icon.
  5. Click Deploy > New deployment.
  6. Click the Select type dropdown and choose Library.
  7. Click Deploy.
  8. Copy the Script ID from the Project settings section.

Example: Reusable Functions Library

Here's a simple example of a Google Apps Script library that contains a reusable function:

  • Open the Google Sheets spreadsheet where you want to use the library functions.
  • Click Extensions > Apps Script.
  • Click Libraries in the Apps Script editor.
  • In the Script ID field, paste the script ID you copied earlier.
  • Click Look up.
  • Click Add.
    // Note: Replace FormattingAndSummingFunctions with the actual name of your library as it 
    appears in the Libraries section.

    function useLibraryFunctions() {
        // Example usage of the formatRange function
        FormattingAndSummingFunctions.formatRange('A1:B2', '#FFFF00', 'bold');
        
        // Example usage of the sumColumn function
        FormattingAndSummingFunctions.sumColumn('A');
      }     

Conclusion

Creating Google Apps Script libraries is a game-changer for developers and users looking to streamline their automation efforts. With reusable code at your disposal, you can save time, ensure consistency, and collaborate more efficiently with your team.

Start creating your own libraries today and unlock the power of reusable code and functions with Google Apps Script!

Tips and Tricks

How to Create QR code using google sheet formula

How to Create QR code using google sheet formula

Dynamically generate QR Code with the help of a little formula.


Read More
How to Convert Column Index to Column Letter

How to Convert Column Index to Column Letter

Converting Column Index to Column Letter in Google Sheets using Google Apps Script


Read More
How to Create Google Forms with Apps Script

How to Create Google Forms with Apps Script

Creating Custom Google Forms with Apps Script Code


Read More
How to Automatically Update Google Calendar

How to Automatically Update Google Calendar

Automatically Update Google Calendar Events with Apps Script


Read More
How to Automate Google Drive

How to Automate Google Drive

Automating Google Drive: Organizing Files and Folders with Apps Script


Read More
How to Create a Google Apps Script Library

How to Create a Google Apps Script Library

Creating a Google Apps Script Library: Reusable Code and Functions


Read More

Start Your Project Now

90

Projects Completed

120

Happy Clients

5

Decoration Awards

240

Coffee Music