Overflow150
Overflow150

Reputation: 5

i want to ask about Google App Script js is not working

Problem 1:

Following is the snipet of a code it takes values from cells in function parameters and process them .

function functionName(Parameter1,Parameter2,ParameterOutput)


{ 
  Parameter1.substr(0, 4);


  Parameter2.toString();


...........


...........

Return ParameterOutput;

}

Parameter1 has digits 544578613

I want to retain only first 4 digits but none of the inbuilt functions are working

.toString()
.substr(0, 4)
.substring(0, 4)

or any other, none of them are working. It still gives output 544578613

TypeError: Cannot read property 'substr' of undefined

Solved by adding folling in the function

var app = SpreadsheetApp;

var activeSheet = app.getActiveSpreadsheet().getActiveSheet();

Problem 2: Second problem is that i checked duplicates in a row by following function. It is perfectly working on first sheet. But i want to modify it for second sheet PLEASE help how can i implement it on second sheet.

image has the function plz click enter image description here

=======================================

Problem 3:

I have function that takes data from column. Runs 1st For loop to retain only first 9 characters. Runs 2nd loop to to pick data from row 1, Runs 3rd nested loop to find dublicates. The problem is that the column has 1400 rows so 2nd loop runs 1400 times & 3rd nested loop runs 1400-minus-minus times with each iteration of outer loop.

It takes a lot of time and google returns Execution Time out error. Google is very slow to perform this task. How can this be solved ?

This function runs fine for rows upto 500

Upvotes: 0

Views: 63

Answers (1)

Raserhin
Raserhin

Reputation: 2676

I don't understand why you are using substr() instead of substring(). What is your source on using substr.

I just tried a simple example and for me getting the 4 first digit with the correct method (substring) worked fine.

function test(){
  var a = "2020notimportant";

  var result = a.substring(0, 4);

  Logger.log(result);

}

2020

Try to use the correct method and see if that works.


This of course can be used with the google sheets cells creating a custom function with very little adjustment.

function TEST(input){
//  var a = "2020notimportant";
  Logger.log(input);
  var a = input.toString(); // Convert to String before `substring`

  var result = a.substring(0, 4);

  Logger.log(result);

  return result;

}

See that I have used toString before substring() in case the input parameter is non-String. Here you can see an example of this working in some cells:

Example of TEST Function

Upvotes: 1

Related Questions