Verifying if a Google Apps Script sheet name or string contains a combination of letters and numbers using JavaScript

I need help with my App Scripts code that checks for sheet names. In my project, there are two types of sheet names - those with only alphabets (e.g. master) and those with a combination of alphabets and numbers (PHY4125). The current code is set to check for the second type of sheet name, but my IF condition isn't working as expected. Is there another approach I can take to achieve the same task?

Here is my code snippet:

function autoConvert(e) {    
    var ss = e.source;
    var sh = ss.getActiveSheet();
    var key = sh.getName();
    var lastRow = sh.getLastRow();
              
    if (key.match(/[0-9]/)) {

        // Code block implementation

    }
}

Answer №1

The Explanation:

This specific regular expression is designed to verify whether the string key contains both letters as well as numeric digits.

Therefore, when this particular expression is utilized like so:

if(key.match(/^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]+$/))

It will return a value of true in scenarios such as AB23423, ab23423, 23233ab, 24443AB, or any other similar combination like As23Abc.


The Solution:

 function autoConvert(e) {    
        var ss=e.source;
        var sh=ss.getActiveSheet();
        var key=sh.getName();
        var lastRow=sh.getLastRow();

       if(key.match(/^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]+$/)){
  
    //Place your code snippet here
  
  }
     
  }

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

Building a dynamic form in React: Fetching select data from an API, posting to another API, and automatically clearing fields upon submission

I am currently working on a form that utilizes a GET request to retrieve data from an API endpoint and then proceeds to make a POST request to another endpoint. Although I have been successful in achieving this function, I am facing challenges with reset ...

The concept of DIV layers and utilizing the jquery click function

My project includes a cool postcard feature that involves animating an overlay div and displaying a postcard on top of it. Here's a snippet of the HTML code: <div id="overlay"> <div class="postcard"> <p>This is a postcar ...

Additional headers are included in the Access-Control-Request-Headers

I have been struggling to include a customized header in my angular js GET request like so: $http({ method : 'GET', url : s, headers : { "partnerId" : 221, "partnerKey" : "heeHBcntCKZwVsQo" } ...

Error: StalePageException occurred in the wicket framework

I am currently using Wicket version 6.20. Within a Wicket page, I have implemented an AbstractDefaultAjaxBehavior to capture mouse clicks and their x,y coordinates: class CallFromJavaScript extends AbstractDefaultAjaxBehavior { private static final l ...

What is the reason my answer for the powerset problem is flawed? Both my recursive and iterative methods are attached for review

How can I generate all possible subsets from an array of unique integers? For instance, if I have powerSet[1,2,3], the expected output should be [[], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]] I've tried a recursive approach: function powerset(arr ...

Can you explain the variance between "+" and "+"?

When needing to use the special character +, is it necessary to use "+" or "\+"? I performed a comparison and found that they both yield the same result. console.log("Comparison Result: " + ("\+" == "+")); //returns true Is the backslash ...

Utilizing Google Analytics within an Angular Single Page Application

After implementing Google Analytics (GA) in my Angular single page application and placing the tracking code at the bottom of the main index.html, I encountered an issue regarding incorrect pageview results. <script> (function(i,s,o,g,r,a,m){i["Go ...

What is the best way to implement a shared function across two classes?

I have a jQuery code that works as intended with the .foto class. Now, I need to add the .fotoleft class to perform the same function. How can I make the function work for two different classes? $(document).ready(function() { $(".foto").click(functi ...

Is scrollTo completely unresponsive?

As a developer specializing in Salesforce, I have successfully implemented a scroll feature that allows users to swipe up and down on all browsers and devices such as iPhone, iPad, and Android. However, when the same page is viewed in the Salesforce1 app, ...

Deactivate the button in the final <td> of a table generated using a loop

I have three different components [Button, AppTable, Contact]. The button component is called with a v-for loop to iterate through other items. I am trying to disable the button within the last item when there is only one generated. Below is the code for ...

Tips for creating a flexible Cell component in react-big-calendar:

Is there a way to make the cell of react-big-calendar flexible depending on the number of events, and enable scrolling? Here is a screenshot showcasing my issue: https://i.stack.imgur.com/K2h69.jpg ...

Transfer information from an array to a Vue function

Having some difficulties passing data to the function myChart within the mounted section. As a beginner in vuejs, I'm struggling with identifying the issue. I am trying to pass data in labels and datasets, which are called from my function. Can anyone ...

Updating the state of Formik

Currently, I'm knee-deep in a React project that requires a slew of calculations. To manage my forms, I've turned to Formik, and for extra utility functions, I've enlisted the help of lodash. Here's a peek at a snippet of my code: impor ...

Error in Syntax: Unforeseen symbol (Initial code)

Encountered Error: Unexpected token < Attempting to call a JavaScript function with console.log("hello world"); I am taking my initial steps into coding in Node.js without any frameworks, focusing on understanding the core and basic concepts of Node.j ...

Transferring data from an uploaded excel file to Python using ajax

I currently have a feature that allows users to upload an excel file using the choose file option. Below is the code snippet for this functionality. HTML <div class="col-md-6"> Upload Excel File <input type="file" id="file_upload" class= ...

Obtain the bounding box of an SVG element while it is not visible

I've been grappling with this issue for more than a day now, but I'm still unable to find a solution. My challenge lies in the need to scale an SVG image for responsive design purposes. Since I have to manipulate the SVG code on the client side, ...

Is it possible to change the color of multiple elements at once using Javascript?

Hi there! I've been working on this code that allows me to change the color of the sidebar on an HTML page using a color picker and saves it to local storage. But now, I'm having trouble figuring out how to use the same code to change the color ...

Issue with JavaScript Replace not replacing second '<' character

I am attempting to swap out the < and > symbols of HTML tags with &gt; & &lt; by utilizing JavaScript. This is the code I am using: var sval = val.replace('&', '&amp;'); sval = sval.replace("<", "&lt;") ...

Choose and Send pictures through a Form with JavaScript

Currently, I am exploring different methods for allowing users to submit a form with the images they have selected. My idea is to present users with a set of images from which they can choose multiple options. Potential Solution 1: One approach could invo ...

Browser and contexmenu intersecting halfway

I have successfully implemented a custom context menu in my HTML project. It functions well, but I am facing an issue where half of the menu appears off-screen to the right. Is there a way to detect this and reposition the menu above the mouse pointer? He ...