Assistance with utilizing Google Sheets scripts for retrieving formulas is needed

I've been working on a small search tool in Google Sheets as a personal project to improve my skills and knowledge of GS. Munkey has been guiding me over the past few weeks and helping me develop my understanding.

Below are the links to my "Database" and search tool:

Database - https://docs.google.com/spreadsheets/d/1K53LOopwAJuOVPJ5RXgpmEO7L3JPHnW5Fx2qp6_3kqo/edit?usp=sharing

Search tool - https://docs.google.com/spreadsheets/d/1mY13e-75dBYfKgkjV8dFFFEvxC838nGNxPrUdusc0PA/edit?usp=sharing

Below is the Script that transfers data from the Database to the search tool databases for the Vlookup function:

function getOvenDataV4(){
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Export Data')
      .addItem('Export Oven Data Now', 'getOvenDataV4')
      .addToUi();

  var settingsSheetName = "oven_settings";
  /* DO NOT EDIT BELOW THIS LINE */  

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var settings = ss.getSheetByName(settingsSheetName);
  var targetSheet = settings.getRange("C2").getValue(); 

  var sSheets = settings.getRange("B2:B").getValues(); 
  var sourceSheets = []; 

  for(var i = 0; i < sSheets.length;i++){
    if(sSheets[i][0]!=""){
       sourceSheets.push(sSheets[i]);
    }
  }

var dKeys = settings.getRange("A2:A").getValues(); 
var sKeys = []; 

for(var i = 0; i < dKeys.length;i++){
  if(dKeys[i][0]!=""){
    sKeys.push(dKeys[i]);
 }

}

var data = []; 

for (var i= 0; i<sourceSheets.length;i++){

  var values = ss.getSheetByName(sourceSheets[i]).getDataRange().getValues();     

  for (var x = 1;x < values.length; x++){
      if(values[x][0]!= ""){
        data.push(values[x]);
      }
  }
}

for (var i = 0; i< sKeys.length;i++){
    var tss = SpreadsheetApp.openById(sKeys[i]);
    var target =  tss.getSheetByName(targetSheet);
    var range = target.getRange(2,1, data.length, data[0].length);    
    target.getRange(2,1,target.getLastRow()-1,target.getLastColumn()).clearContent(); 
    range.setValues(data); 
  }
}

Munkey provided some helpful comments to the script above to aid my understanding. Although the script works well, it doesn't pull data from my =image("") formula in column "H".

Is there anyone who could assist in modifying the script to include the =Image("") formula in the search tool, similar to how =importrange functions? I've tried various methods like getFormula and getFormulas, but with no success so far. It's likely I'm just not using them correctly :)

Answer №1

This solution may not be the most elegant, but it gets the job done by organizing the data into an array called row. This array includes both values and formulas as required.

I am open to suggestions on how to enhance this solution further.

function getOvenDataV6() {

  var settingsSheetName = "monkey_settings";

  /* DO NOT EDIT BELOW THIS LINE */

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var settings = ss.getSheetByName(settingsSheetName);

  var targetSheetsValues = settings.getRange("C2:C").getValues();
  var targetSheets = [];

  var sSheets = settings.getRange("B2:B").getValues();
  var sourceSheets = [];

  for (var i = 0; i < targetSheetsValues.length; i++) {
    if (targetSheetsValues[i][0] != "") {
      targetSheets.push(targetSheetsValues[i]);
    }
  }

  for (var i = 0; i < sSheets.length; i++) {
    if (sSheets[i][0] != "") {
      sourceSheets.push(sSheets[i]);
    }
  }

  var dKeys = settings.getRange("A2:A").getValues();
  var sKeys = [];

  for (var i = 0; i < dKeys.length; i++) {
    if (dKeys[i][0] != "") {
      sKeys.push(dKeys[i]);
    }
  }

  var data = [];
  for (var i = 0; i < sourceSheets.length; i++) {
    var values = ss.getSheetByName(sourceSheets[i]).getDataRange().getValues();
    var formula = ss.getSheetByName(sourceSheets[i]).getDataRange().getFormulas();

    for (var x = 1; x < values.length; x++) {
      var row = [
        [values[x][0]],
        [values[x][1]],
        [values[x][2]],
        [values[x][3]],
        [values[x][4]],
        [values[x][5]],
        [values[x][6]],
        [formula[x][7]]
      ];

      if (values[x][0] != "") {
        data.push(row);
      }
    }
  }

  var headers = [
    ["Model No", "Product Code", "Brand", "Model No", "kW", "Amp", "Apeture", "Image"]
  ];

  for (var i = 0; i < sKeys.length; i++) {
    var tss = SpreadsheetApp.openById(sKeys[i]);
    for (var x = 0; x < targetSheets.length; x++) {
      var target = tss.getSheetByName(targetSheets[x]);
      var range = target.getRange(2, 1, data.length, data[0].length);
      target.clearContents();
      target.getRange("A1:F1").setValues(headers);
      range.setValues(data);
    }
  }
}

The main modification in the code is:

 for (var i= 0; i<sourceSheets.length;i++){

    var values = ss.getSheetByName(sourceSheets[i]).getDataRange().getValues();
    var formula = ss.getSheetByName(sourceSheets[i]).getDataRange().getFormulas();

    for (var x = 1;x < values.length; x++){
      var row = [  [values[x][0]], [values[x][1]], [values[x][2]], [values[x][3]],[values[x][4]],[values[x][5]],[values[x][6]],[formula[x][7]]   ];     

      if(values[x][0]!= ""){
        data.push(row);
      }
  }
}

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

What is the best way to fetch JSON data in React from the start and then manipulate it as needed before exporting to another file within my application?

I have a scenario where I need to fetch data from a MongoDB database using async/await fetch() and save it in a file called data.js. However, I am facing an issue with React executing "in parallel" due to the nature of async/await. This makes it difficult ...

What could be causing my jQuery handler to not capture my form submission?

I am developing a Ruby web application and using JQuery and AJAX to send/receive data. However, I am facing an issue where pressing the enter key does not submit the form. What should I do to ensure that my form submits successfully? Within my Foundation ...

Using a JSON key as a parameter in a function

Would it be achievable to specify the key of an object as a function parameter? For instance, if I were to develop a filter function that could sort multiple map markers by marker.element.country or marker.element.population? This approach would allow me ...

Guide on utilizing exported API endpoint in Node and Express

Seeking a deeper understanding of express and its utilization of various endpoints. Recently came across an example of an endpoint that reads in a json file, demonstrated as follows: const fs = require('fs'); const path = require('path&apos ...

Guide on changing the order of Vue sibling components when rendering a shared array within a parent component list

Currently facing a unique challenge and seeking input: Within the 'App', utilize 'TestListItem' for odd item indexes and 'TestListBetterItem' for even indexes. The same index must be used for both components. Initial attemp ...

Error TS2346: The parameters provided do not match the signature for the d3Service/d3-ng2-service TypeScript function

I am working with an SVG file that includes both rectangular elements and text elements. index.html <svg id="timeline" width="300" height="100"> <g transform="translate(10,10)" class="container" width="280" height="96"> <rect x ...

The model fails to bind when JSON is sent to the MVC controller

I've been facing an issue with my JSON payload not getting binded in my controller. I attempted creating a class with List<Models.UpdateChatRequestModel> Chats, but that didn't work for me. I also tried using an array name, but that approac ...

Elegant switch in jQuery

I am trying to use jQuery to create an animated toggle button. The toggle function is working correctly, but I am having trouble adjusting the speed and smoothness of the animation. After researching different methods and attempting to modify the values i ...

What is the best method for swapping out an iframe with a div using Javascript?

Having an issue with loading an external HTML page into an iFrame on my website. Currently facing two main problems: The height of the iFrame is fixed, but I need it to adjust based on the content's height. The content inside the iFrame does not inh ...

Images showing Strava heat maps retrieved through API

Check out this amazing heatmap created by Strava! I'm curious about how they were able to achieve this - it seems like they are using the API to request overlay images based on the network tab. I have my own geo data, but I'm wondering how I can ...

What is the best way to retrieve the value of a text box in VUEjs using its unique identifier?

In my form, there are a total of two text boxes with predefined values. However, I am looking for a way to retrieve the value of one specific textbox based on the entered ID number. For example, if I input "1," I expect to see the value of text box 1 only ...

Struggling with obtaining react-modal in my React Component

Greetings to all React developers out there, especially the newbies like me. I am currently facing an issue with implementing react-modal in my React Component based on this example here. Unfortunately, I have encountered several errors that are proving di ...

Trouble with the x-cloak attribute in alpine.js

Experience with TailwindCSS and AlpineJS in my current project has brought to my attention a slight issue with the header dropdowns flashing open momentarily when the login page loads. I attempted to use x-cloak to address this, but encountered some diffic ...

Determining the orientation of an image in JavaScript

Currently, I am attempting to determine the orientation of images using JavaScript in order to apply a specific class to them. This process seems to be functioning correctly on Firefox, but is presenting challenges on other browsers. It appears to work bet ...

Are memory leaks a common issue with Angular directives?

Using a simple html file to replicate a memory leak: <!doctype html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.min.js"></script> <script> va ...

Currently, I am working on a project and encountering an issue with html2canvas

const handleDownloadImage = async () => { try { const canvas = await html2canvas(cardRef.current); console.log(cardRef.current); const imageData = canvas.toDataURL("image/png"); setImageUrl(imageData); } catch ( ...

Having trouble loading an image after successfully connecting to an API with react.js

I've been working on a custom fetch component to load images from the "the dog API" onto my page. However, I'm facing some issues with getting the images to display correctly. Can anyone spot what might be missing in my setup? App.js import &apo ...

Jest does not allow mocking a module and validating function invocations at the same time

After setting up a new project using create-app-component, which includes build scripts (babel, webpack, jest), I proceeded to write a React component that requires another javascript file containing a function. The contents of my search.js file are as fo ...

Retrieving the value of a selected radio button in Asp.net using Javascript

I have searched for a solution to my issue multiple times, but none of the solutions I have found seem to work for me. I am currently working with IE. <asp:RadioButtonList ID="rbtnView" runat="server" OnSelectedIndexChanged="rbtnView_SelectedIndexChan ...

What is the method for accessing an app from a file other than server.js?

I am dealing with two different files: file1.js const app = express(); require('./file1/config/customConfigurations').customSettings() .then((settings) => { app.locals.customSettings = settings; console.log(app.locals.customSettings) ...