Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8358281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:44:46+00:00 2026-06-09T10:44:46+00:00

I’m trying to find out two things. Can I optimize my code so that

  • 0

I’m trying to find out two things.

  1. Can I optimize my code so that it will run faster
  2. Is there a way to test the speed of my script.

My script is simply pulling data from a URL (Example: https://www.sciencebase.gov/catalog/items?q=Water&max=100&format=json) that will be pasted into the first cell of a spreadsheet. It then populates the cells with the first column storing the title and the second column storing the summary.

I’m using a trigger that will execute the script onEdit() so that it will run whenever a edit is made to the first cell of the spreadsheet.

Any pointers, tips, or even a go look here is appreciated

Here’s my code,

function respondToSearch() {

     // Gets the active sheet in a spreadsheet
     var ss = SpreadsheetApp.getActiveSpreadsheet(); 

     // Gets sheet 1 and sets the first column to a width of 200 and the second 
     column to width 500
     var sheet = ss.getSheets()[0].setColumnWidth(1, 200).setColumnWidth(2, 500);
     var activeSheet = SpreadsheetApp.getActiveSheet();

     // Gets the value from the top left cell in a range
     var dataRange = activeSheet.getDataRange().getValue();

     // Sends an HTTP request to fetch the URL
     var searchResponse = UrlFetchApp.fetch(dataRange);

     // Get the response as a string and parse (string is in JSON format) 
     var parsedResponse = Utilities.jsonParse(searchResponse.getContentText());

     // Array to hold letter corresponding to spreadsheet columns
     var cellLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", 
     "m", "n", "o", "p", "q", "r", "s", "t"];

     // Loop through the "item" objects and display the properties in the 
     spreadsheet
     for (var i = 0; i < 2; i++) {  
       for (var j = 0; j < parsedResponse.items.length; j++) {      

       // Starts with items[0] (1st object) and displays the properties
       var parsedItems = [parsedResponse.items[j].title, parsedResponse.items[j]
       .summary];

       // If a property is not undefined print the property in the spreadsheet 
       else print "N/A"
       if (parsedItems[i] != undefined) {
         var print = parsedItems[i];
       }
       else {
         var print = "N/A";
       }

       // Stores the current letter corresponding to the current spreadsheet column
       var cellLtr = cellLetters[i];

       // Starts the row available to display data at number 2
       var cellNum = [j + 2];
       var cell = cellLtr + cellNum;

       sheet.setActiveCell(cell).setFontSize("9").setHorizontalAlignment("left")
       .setVerticalAlignment("top").setValue(print);
      }
     }
    } 

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:44:48+00:00Added an answer on June 9, 2026 at 10:44 am

    Can I optimize my code so that it will run faster

    I think it is possible. Try to rewrite your code to set the font size and the alignments not for every cells but for the whole range.

    Is there a way to test the speed of my script.

    Yes. The code should looks line the following

    var start = new Date();
    my_time_consuming_function();
    var diff = new Date() - start;
    Logger.log("my_time_consuming_function takes: " + diff + " ms");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.