Seek out the value and retrieve the corresponding row index

I'm facing an issue with this script. My goal is to search for today's date, match it, and return the column number (which is already working!). Now, I need a script that can do the same but with row numbers. The current script works fine if there is only one table on the sheet, however, since I have multiple tables, using values.length -1 will return the last row from the entire sheet. Additionally, each table may not have fixed row numbers, so the solution needs to be dynamic.

Here is the script I have developed so far:

function getTodaysTotal() {
  function toDateFormat(date) {
try {return date.setHours(0,0,0,0);}
catch(e) {return;}
 }

var values = SpreadsheetApp
  .openById("id")
  .getSheetByName("Q3 - W27 - 39")
  .getDataRange()
  .getValues();


var today = toDateFormat(new Date());
var todaysColumn = values[5].map(toDateFormat).map(Number).indexOf(+today);
var output = values[values.length - 1][todaysColumn];
var emailDate = Utilities.formatDate(new Date(today),"GMT+1", "dd/MM/yyyy");

Attached below is a screenshot of my table:

table

I hope this clarifies the situation. I already have the column number, now I just need to find the row number that contains "Total".

Thank you!

Regards,

Answer №1

If you want to achieve optimal speed, the best approach is to iterate through an array of data.

Check out the script below and integrate it into your existing code.

Assuming you are focusing on a single column, such as column C, here's an example:

for (i in values){
if (values[i][2]=='Total'){
Logger.log(i);
var nr = i
   }
}

The variable "nr" now stores the row number where the word "Total" was found in column C (indicated by [2] after [i]).

You can then utilize this variable for any purpose following the initial closing bracket.

Answer №2

Upon incorporating this into my script, I encountered an issue where the output returned as undefined. Interestingly, when utilizing the logger log function, I was able to obtain the correct value, however, it also resulted in an empty value (undefined). Consequently, I only require the initial value.

function getTodaysTotal() {
 function toDateFormat(date) {
try {return date.setHours(0,0,0,0);}
catch(e) {return;}
   }

var values = SpreadsheetApp
  .openById("id")
  .getSheetByName("Q3 - W27 - 39")
  .getDataRange()
  .getValues();

for (i in values){
 if (values[i][0]=='Total'){
 var nr = i;
 var newNr = parseInt(nr);

 //       Logger.log(nr);


var today = toDateFormat(new Date());
var todaysColumn = 
 values[5].map(toDateFormat).map(Number).indexOf(+today);
var output = values[newNr][todaysColumn];
 Logger.log(output);
var emailDate = Utilities.formatDate(new Date(today),"GMT+1", "dd/MM/yyyy");
//   Logger.log(todaysColumn);
//    Logger.log(output);
// return values[values.length - 1][todaysColumn];
}
 }




 if (output == undefined) {
GmailApp.sendEmail("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="12777f737b7e52717d7f62737c6b3c717d7f">[email protected]</a>", "test data", "Today, " +emailDate +" we had no calls made or no values are inputed.");   
 }
 else if (output == "") {
GmailApp.sendEmail("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7d2dad6dedbf7d4d8dac7d6d9ce99d4d8da">[email protected]</a>", "test data", "Today, " +emailDate +" we had no calls made or no values are inputed.");
}
 else {

GmailApp.sendEmail("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20454d41494c60434f4d50414e590e434f4d">[email protected]</a>", "test data", "Today, " +emailDate +" we had " +output + " calls made.");

}
 }

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

angular model bind include move

I'm working on developing custom elements that will transform my form elements to match the styling structure of Bootstrap's forms. Essentially, <my-input ng-model="myname"> should be transformed into <div class="form-element"> ...

Can I deactivate JavaScript on my website directly from my server settings?

I'm currently attempting to link my Android application to a PHP script hosted on a free server. However, when my app tries to access the page, I receive an HTML message stating that JavaScript is disabled and needs to be enabled in order to view the ...

I'm having trouble with the colors not displaying correctly on my NextJS Tailwind navbar

I've encountered an issue with my navbar where none of the specified colors are being displayed. The code snippet for the navbar is as follows: Codes: 'use client' import Head from 'next/head'; import Link from 'next/link&apo ...

"Revolutionary AJAX-enabled PHP social commenting system with multi-form support

Why is it that when I submit forms using these ajax functions in PHP, they only send to the first form on the page? I have multiple forms under each article and I want them to be submitted separately. What am I doing wrong? ...

Ways to have a list component smoothly descend when a dropdown menu is activated

I have a situation where I have a list with two buttons, and each button triggers the same dropdown content in a sidebar component. The issue is that when I click on one button to open the dropdown, the second button does not move down to make space for it ...

Retrieve the complete content of a web page, including all HTML and JavaScript elements

After spending several hours researching and experimenting, I find myself a bit confused about the topic at hand. My issue: I am attempting to retrieve the complete HTML content (including dynamically generated JavaScript content) of a specific web page. ...

Load images in the background prior to displaying them

Currently, my JavaScript/jQuery code is cycling through different background images like this... var duration = 2500; var delay = 500; var i = 0; setInterval(function() { if (i == 0) { $(".myimage").css("background-image", "url('https://pla ...

Transitioning From Browserify to Webpack with Vue.js

We currently use a combination of Grunt, vueify, and browserify for our build process. This setup builds our Single File Components and separates Vue into its own external file. Due to various factors like vueify being unsupported and the need for async c ...

Is there a way to arrange the characters in a string using Pointer Arrays?

I have been working on a C programming program to sort characters within a string and also sort a list of strings alphabetically. I have successfully sorted the list of strings, but for some reason, the individual characters within each string are not bein ...

Is it possible to install the lib ldap-client module in node.js?

I'm having trouble installing the lib ldap-client package in Node.js. To try and solve this issue, I consulted the following page: https://github.com/nodejs/node-gyp. In an attempt to fix the problem, I have installed python, node-gyp, and Visual St ...

Transform mysql data in bulk to json format with php

I am struggling to convert a large amount of MySQL data into JSON using PHP. With over 20,000 records, I can't seem to successfully convert the MySQL data into JSON format for my REST API. Here is the code I have attempted so far: $query = mysql_qu ...

What is the best way to conceal a bootstrap directive tooltip in Vue.js for mobile users?

Currently, I'm tackling a project with Vuejs and Laravel. There's a tooltip directive incorporated that relies on Bootstrap's functionality. Check it out below: window.Vue.directive("tooltip", function(el, binding) { // console ...

Styling the scrollbar for the PDF element on an HTML page

I have a div that contains an object "linked" to a .pdf file. Is it possible to customize the scrollbar style using CSS or JavaScript/jQuery? <div id="container"> <object data="document.pdf" type="application/pdf" ...

Step-by-step guide for adding an icon to the corner of a Material UI button

Is there a way to position an icon in the corner of a Material UI button in React? Currently, I have the icon next to the title but I would like to move it to the lower right corner of the button. Any suggestions on how to achieve this? Thank you! export ...

Accessing the 'comment' property within the .then() function is not possible if it is undefined

Why does obj[i] become undefined inside the .then() function? obj = [{'id': 1, 'name': 'john', 'age': '22', 'group': 'grA'}, {'id': 2, 'name': 'mike', &apo ...

Determine whether a subdomain is present within an ajax file in php

Looking for assistance with checking the existence of a 'Subdomain' in a file called '\example\sites'. Here's the code: $form = array() ; $form['name'] = "install"; $form['action'] = "?step=2"; $for ...

Issue: Bidirectional binding functionality is not functioning properly within the user interface modal

I'm currently working on displaying data in a modal, but I'm facing issues with getting two-way binding to work properly. In my HTML code: <div class="cta-actions"> <div class="action"><a class="btn btn-lg btn-max">Send a pa ...

Is there a way to connect a Button to a different page in VUE.JS?

I currently have a button that needs to be linked to another page. This is the code I am working with at the moment. How can we write this login functionality in vue.js? It should direct users to the page "/shop/customer/login" <div class="space-x ...

The camera application is functioning smoothly on Marshmallow API 23, however, it crashes when trying to use the camera on API versions higher than 23

After creating a simple app for capturing photos and storing them in the SURVEYASSIST folder on the SD card, everything was running smoothly on my Redmi 3s prime (Android version Marshmallow 6.0.1). However, upon opening the app on the Redmi Note 5 pro (Or ...

Switching Between Background Images in Angular

Looking to change the background-image of an element upon clicking it. Currently, this is what's in place: <div class="item-center text-center" toggle-class="active cable"> <div class="quiz-background"></div> <p class="size ...