Enhance the output of an array by incorporating additional text into the values

I'm diving into the world of Apps Script and I have a task at hand - adding some text to each value in an array. However, the code I currently have simply moves those values to another column:

function getData() {
  var sheet1 = SpreadsheetApp.getActiveSpreadsheet()
      .getSheetByName("Stocks");
  var symbol = sheet1.getRange('A1:A7').getValues();
  sheet1.getRange('C1:C7').setValues(symbol);
}

What I really want is to enhance the output by appending additional text to each value, something like this:

function getData() {
  var sheet1 = SpreadsheetApp.getActiveSpreadsheet()
      .getSheetByName("Stocks");
  var symbol = sheet1.getRange('A1:A7').getValues();
  sheet1.getRange('C1:C7').setValues(
      '=GOOGLEFINANCE("FRA:' + symbol + ")'
  );
}

Unfortunately, I'm aware that my current approach won't yield the desired results. How can I successfully append text to each value being written?

Answer №1

Utilize a loop to iterate through the array of values

function fetchData() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Stocks");
  var symbols = sheet.getRange('A1:A7').getValues();
  var destinationRange = sheet.getRange('C1:C7');

  for (var i = 0; i < symbols.length; i++)
    symbols[i][0] = '=GOOGLEFINANCE("FRA:' + symbols[i][0] + '")');

  destinationRange.setFormulas(symbols);
}

Since ranges A1:A7 and C1:C7 have the same dimensions, you can reutilize that array and then use setFormulas(array). If you plan to reuse the symbols array multiple times, declare an empty array before your loop as follows:

var dataArray = []

Inside your loop, push the data into this new array:

dataArray.push(['=GOOGLEFINANCE("FRA:' + symbols[i][0] + '")']);

Then, outside the loop, use the setFormulas() method:

destinationRange.setFormulas(dataArray);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporate a custom style sheet into your Express application

Utilizing ejs, express, and parse.com for my web application backend has presented a challenge when it comes to adding stylesheets. Despite searching for solutions, I have not been able to resolve the issue. Sharing my code in hopes of finding a solution. ...

What is the method to retrieve a javascript object from within a string template?

I am trying to find a way to map data from an object into a string template using JavaScript. Here is an example of the string and object I am working with: const menu = { breakfast: { description:'something' } meal: { d ...

Display a portion of the existing URL as a clickable link on the webpage using JavaScript or PHP

If I have a website with the URL and I would like to showcase the image at https://example.com/image.jpg on my site (), what can I do? I attempted the following approach, but it only displays the URL of the image. <p id="image"></p> <scri ...

Transform the constant character pointer into an integer array

Currently, I am experimenting with the C++ MongoDB driver Below is the code I am testing: http://pastebin.com/eQUekQU2 In the code, I create a small integer array and store it in the mongo database as binary data. To retrieve the binary data while loop ...

Is there an alternative method to retrieve the client's user agent if getStaticProps and getServerSideProps cannot be used together?

I am currently facing a challenge with the website I'm working on as it lacks a responsive design. This means that the view I display is dependent on the user agent of the client. In order to achieve this, I have been using getServerSideProps to deter ...

Exploring through a table using JavaScript

I am struggling to search a table within my HTML for a specific term. The code I have works to an extent, but it does not account for alternatives such as searching for "what is that" when I type in "What is that". Additionally, I need the script to ignor ...

What issues can be found in this code snippet for AngularJS in the browser code editor that is interacting with Node.js

Greetings, I am currently working on developing a free in-browser code editor for high school students at the basic level to share knowledge. After extensive research, I came across this link. Following the instructions provided, I made some setups and mod ...

Tips for managing React state when dealing with multiple axios callbacks that modify an array's state

I am currently working on a React component that allows users to upload images. In this scenario, I aim to upload 3 images. After each file is uploaded, I want to append it to the list of uploaded files. Issue: The setter method in useState is asynchronou ...

What is another option for toggling in jQuery?

After deprecating the toggle method, I found a new way to toggle div: $("#syndicates_showmore").on("click", function () { if (!clicked) { $('#syndicates').fadeTo('slow', 0.3, function () { $(this).css( ...

"Implementing a function in React to update the state based on target name and value received from

In an attempt to update the state, the current function (handleLabelChange) technically works by updating the state. However, I am looking to dynamically add a name instead of textOne and a value of "????" from the textarea field or another input field. It ...

Having trouble getting gulp set up and running with npm?

I am currently in the process of setting up gulp using npm in order to execute my project. Based on my understanding, all I need to do is enter "npm install gulp" in the command line at the location of my project like this : https://i.stack.imgur.com/hPU ...

jQuery functionality restricted to desktop devices

I am attempting to disable a jQuery function specifically for mobile devices. I found some instructions that seem helpful on this page. Unfortunately, following the instructions did not work for me. Here is the code snippet I have: var isMobile = /Androi ...

Comparing jQuery AJAX with UpdatePanel

Our team is facing the challenge of dealing with a page containing an overwhelming amount of jQuery code (specifically around 2000 lines) that has become difficult to maintain. We are considering shifting some of this functionality to the server side for ...

What is the most efficient way to calculate the current percentage of time that has elapsed today

Is there a way to calculate the current time elapsed percentage of today's time using either JavaScript or PHP? Any tips on how to achieve this? ...

Leveraging Parameters from URL in Javascript

I'm facing an issue with my area shape, the href="/kosmetikstudios/deutschland/Bayern" tag seems to be causing a problem. I want to utilize the parameter "Bayern" (which is the last parameter in the URL). I need this to be dynamic. Below is my JavaS ...

Python is not formatting "n" and "k" with brackets as I intended

if __name__ == "__main__": d = len(sys.argv)>3 n = int(sys.argv[1]) k = int(sys.argv[2]) A = [] for i in range(n): A.append(i) if d: print("[{0} {1}]".format(n,k)) val = combinations(A, k) for i in val: print(i) I have implemented this pie ...

Prevent scrolling in AngularJS model popups

When loading data for the first time in a model popup, the scroll bar is not displayed inside the popup. However, after performing a search function and filtering the data, the scroll bar appears inside the model popup. How can this issue be fixed? this ...

Analyzing two disorganized sets in MongoDB

I need to compare a large number of documents stored in two collections. In total, there are approximately 1300 documents in each of these collections. My goal is to create a diff comparison report after comparing the two collections. My main focus is on ...

Looping through an array

I have created an array as shown below: iArray = [true, true, false, false, false, false, false, false, true, true, true, false, true, false, false, false, false, true] Condition check: If any value in this array is false, I will display an error messag ...

Using res.sendfile in a Node Express server and sending additional data along with the file

Can a Node.JS application redirect to an HTML file using the res.sendFile method from express and include JSON data in the process? ...