Locate elements in a 2D Array with Google Apps Script

I am working with an array containing the values:

[8:00 AM, 9:00 AM] //result of another function

My goal is to locate these values within a 2-Dimensional Array:

[
 [8:00 AM], 
 [9:00 AM], 
 [10:00 AM], 
 [11:00 AM], 
 [12:00 NN], 
 [1:00 PM], 
 [2:00 PM], 
 [3:00 PM]
]

and then update them to "Not Available"

The values in the array are retrieved from a Google Sheet using this script:

function testRow(){

  var lookDate = "Aug 28, 2019";
  var ss = SpreadsheetApp.openByUrl(url); 
  var ts = ss.getSheetByName("Time_Select");
  var checkData = ts.getRange(1, 1, 1, ts.getLastColumn()).getDisplayValues()[0];

  var index = checkData.indexOf(lookDate)+1;

  var timeValues = ts.getRange(2, index, ts.getLastRow()-1, 1).getValues();
  Logger.log(timeValues)

  /*var checkSplit = dateValues.join().split(",");
  var checkMe = checkSplit.indexOf(dataDisable[1]);
  var timeValues = ts.getRange(checkMe, index).getValue();*/
}

I attempted using this code (as shown above in the Google Script):

var checkSplit = dateValues.join().split(",");
var checkMe = checkSplit.indexOf(dataDisable[1]);
var timeValues = ts.getRange(checkMe, index).getValue();

but it did not give me the correct result. Do you have any suggestions or solutions on how I can search for values within the 2-dimensional array, find their location in the Google Sheet, and change the value to "Not Available"? Thank you in advance for your assistance.

here is the link for my google sheet: https://docs.google.com/spreadsheets/d/1lEfzjG1zzJVPMN8r-OpeZm6q9_IqSwk9DNCEY-q7ozY/edit?usp=sharing

Answer №1

Your timeValues output is in string format. Assuming that the elements within [8:00 AM, 9:00 AM] are also strings, here is a method to compare them and replace cell values with "Not Available" if they match:

function verifyRow(){

  var selectedDate = "Aug 28, 2019";
  var spreadsheet = SpreadsheetApp.openByUrl(url); 
  var timeSheet = spreadsheet.getSheetByName("Time_Select");
  var dataToCheck = timeSheet.getRange(1, 1, 1, timeSheet.getLastColumn()).getDisplayValues()[0];

  var position = dataToCheck.indexOf(selectedDate) + 1;
  
  var times = ['8:00 AM', '9:00 AM'];
  var timeValues = timeSheet.getRange(2, position, timeSheet.getLastRow() - 1, 1).getValues();
  
  for(var i = 0; i < times.length; i++){
    for(var j = 0; j < timeValues.length; j++){
      if(times[i] == timeValues[j][0]){
        timeValues[j][0] = "Not Available"
      }
    }
  }
  timeSheet.getRange(2, position, timeSheet.getLastRow() - 1, 1).setValues(timeValues);
}

In essence, you iterate through both arrays and replace matching elements in timeValues with "Not Available". Upon completing the loops, update your range with the modified values from the timeValues array.

Answer №2

What if we try this instead:

let updatedValues = timeValues.map((_value) => { return [_value]; });

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 yields greater performance in MongoDB: employing multiple indexes or creating multiple collections?

Currently, I am developing an application that validates users through various 3rd party services such as Facebook and Google. Each user is assigned a unique internal id (uuid v4) which corresponds to their 3rd party ids. The mongoose schema for my user do ...

"How to ensure consistent styling for all buttons, including dynamically created ones, in an application by utilizing the jQuery button widget without the need for repetitive calls to

Hello everyone, I am a newcomer to stack overflow and I have a question to ask. Please excuse any errors in my query. I have searched for an answer but have not been successful in finding one so far. Let's say I have a webpage where I am using the jQ ...

Troubleshooting jQuery's issue with dynamically adding input fields

I came across a tutorial (which I tweaked slightly) on this website: code In JSFiddle, everything works perfectly fine with the code. However, when I implemented it on my actual page, it's not functioning as expected. I've been trying to trouble ...

Struggling to display a navigation menu on angular 1 with complex nested json structure

I have set up a navigation bar using JSON data fetched by an Angular service. Everything is working fine with my service and controller, but I am facing difficulty in displaying the nested JSON data in my view. Here is the JSON data: { "menu": [ ...

How can Reactjs access a configuration file?

I am struggling to find a solution for reading a properties file in reactJS. I have come across the "properties-reader" module but I can't figure out how to make the require function work. Is there an easier way? For instance, import React, { Com ...

Struggling with a findIndex problem in React and JavaScript

Whenever I receive activeFilters values like this: const filter = [...activeFilters] The findIndex function always returns -1 even when the item I'm searching for exists. However, when I receive it like this, it works? const filter = activeFilters ...

Perform an Ajax request with JQuery in an HTML document and transfer the response to a different HTML page

This is my first attempt at using AJAX and jQuery to retrieve data from another HTML page. Below is the code I have written: Here is my JavaScript code: <script type="text/javascript> $(document).ready(function() { $('#submit').click( ...

What is the way to utilize JavaScript for parsing Ruby JSON strings?

I am currently attempting to extract the JSON object from a string that has been outputted in JSON format from a Rails application. In my JavaScript code, I have the following: data = "<%= @chromosomes.html_safe %>"; The issue I am facing is that d ...

Array of Typed Objects in TypeScript

Here is the setter I created in TypeScript: public set data(data: Array<Data>) { console.log(data[0].getterProperty); console.log(data[0] instanceof Data); console.log(typeof data[0]); this.setGridDataIfReady(); } If data contains only one ...

Tips for synchronizing arrays using the Angular Drag-Drop Component

My drag and drop component in HTML looks like this: <div class="example-container flex flex-col text-center h-fit min-h-[10rem] w-[15%] border-2 border-gray-100 rounded-md shadow-md" > <h2 class="text-xl font-semibo ...

Vue 3 - gaining access to the parent component

I recently upgraded from Vue 2 to Vue 3 and encountered an issue with using methods from a parent component in ag-grid buttons within each row. In Vue 2, the syntax was straightforward using this.$parent.$parent due to Ag-Grid. However, in Vue 3, I am stru ...

What could be the reason for the issue with Backbone.js and modal window breaking on IE9?

I have a basic contact management table in backbone.js that utilizes twitter's bootstrap-modal to display the form input. While everything works smoothly in Firefox and Chrome, I am encountering issues with the modal not appearing in IE 9. Additional ...

What is the reason for the malfunction of the top input function?

In Section 1, if I select checkbox bbbbbb or checkbox cccccc, the input will be disabled and it doesn't work. However, in Section 2, everything is functioning correctly. Why isn't the functionality working in Section 1? Thank you. Section 1 & ...

Break up each object in a Java ArrayList into individual elements

Looking to parse each entry in the ArrayList as many entries contain commas. Each entry consists of an item and optional value: Access control enable ,disabled Access policy prototyping ,enabled ...

React-Redux showing no errors despite non-functional Redux store

I'm currently facing an issue with integrating React-Redux into my React Native/Expo project. Despite not receiving any console error messages, the data from the Redux store is not displaying in the user interface. Below are some key files related to ...

Client connected via Socket.io is not receiving the message event

My project was built using express-generator as the skeleton, so I had to implement a workaround which I found here. SERVER: io.on('connection', function(socket){ socket.on('message', function(msg){ io.emit('message', msg) ...

Strategies for managing large numbers within WebGL GLSL shader programs

What is the best approach to handle large numbers like the one provided in GLSL? I have a shader that requires Date.now() as a uniform, which is defined as: The Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 ...

Activate Pop-up for a single instance on BigCommerce

After researching and adding my own code, I am still struggling to get this question answered correctly. Here are the key points I am trying to achieve: 1. Automatically open a popup when the homepage loads. 2. Ensure that the popup is centered on all brow ...

What makes React stand out as a server side rendering technology when compared to Angular?

While delving into the world of ReactJs, I discovered a unique comparison between its rendering aspect and AngularJs. Interestingly, some refer to ReactJs as a server-side rendering technology. This revelation has truly caught me off guard! To explore fu ...

A guide on inserting text annotations into text notes within a React Next.js 14 (with App Router) application

I am currently in the process of developing an application that allows users to provide commentary on legal rulings. Within my app, I have both judgments and notes. The notes are created based on excerpts from the judgments. Right now, when a user highligh ...