Very Stupid Question Regarding .getRange() [duplicate] - google-apps-script

This question already has an answer here:
Cannot find method getRange(number,number)?
(1 answer)
Closed 5 months ago.
I am really sorry.. this is probably going to be the dumbest question you have seen in a good long time, if not ever... LOL I have been pulling my hair out on this for two hours and for the life of me I can't figure out what I am doing wrong...
This is stupid simple. It is a simple SpreadsheetApp.getRange() using the alternate parameters instead of your the usual "A1" reference. I am sure I am screwing up the syntax, but for the life of me I can't seem to figure out or find what it is that I am doing wrong...
This ties in with a larger project, of course, but for the sake of simplicity, this is what I currently have:
function test() {
var teamSheet = SpreadsheetApp.getActive();
teamSheet.getRange("25,25").activate();
teamSheet.getCurrentCell().setValue("Stuff");
};
All I want it to do there is go to Y25 and put in the word "Stuff". This is eventually going to end up in a loop where both the Row and Column values are increasing with each iteration - hence why I am using the alternate parameters instead of just entering "Y25".
I have tried in single quotes, double quotes, no quotes, square brackets, R[25]C[25], on and on... The error message I am getting is either Exception: Range not found or Exception: The parameters (String,number) don't match the method signature for SpreadsheetApp.Spreadsheet.getRange.
I appreciate this is a bit of a waste of your time and I am likely making some stupid, silly mistake - but I don't see it and I am loosing my mind on this... Please help!!

The script you provide always goes to A25 because the current cell of an active range is always the upper left corner. It's the same as this one.
function test() {
const ss = SpreadsheetApp.getActive();
ss.getRange("25:25").activate()
ss.getCurrentCell().setValue("A25");
};
I don't understand why you would want to but you could right it like this:
function test() {
const ss = SpreadsheetApp.getActive();
ss.getRange("Y25:25").activate()
ss.getCurrentCell().setValue("Y25");
};
And then it would go to Y25 and put "Y25" there or just as easily
function test() {
const ss = SpreadsheetApp.getActive();
ss.getRange("Y25").activate()
ss.getCurrentCell().setValue("Stuff");
};
If this does not answer your question let me know. Perhaps you should ask it in the context of how you wish to apply it.

Related

Problems with body.findText in Google apps script

I have a Google doc which was created in Word and imported. I want to locate a 'marker' in the text so that I can insert a table at that point. However, I am unable to progress because my script keeps falling over at the "findText()" point. I have looked at a number of answers to similar problems on here and used what I think are identical methods, but it still falls over!
Rather than post code here, it seems clearer to post a screenshot of exactly what I get when I attempt to run the script. The first two items in the execution log prove that a) the document is being opened correctly and b) that the body is being correctly identified. There is definitely a "$" sign later in the text, so why am I getting the error?
Script
var doc = DocumentApp.openByUrl("https://docs.google.com/document/d/###/edit");
var body = doc.getBody();
var tblLoc = body.findText("$");
console.log(tblLoc)
Modification points:
From your following image,
I thought that the reason of your issue is due to $ of body.findText("$").
In this case, I think that it is required add \\ like body.findText("\\$")
The method of findText() returns the object of DocumentApp.RangeElement. So when you use console.log(tblLoc) and when the value is retrieved, {} is shown in the log.
When you want to see the retrieved value, for example, you can use console.log(tblLoc.getElement().asText().getText()).
When above points are reflected to your script, it becomes as follows.
Modified script:
const doc = DocumentApp.openByUrl("https://docs.google.com/document/d/###/edit");
const body = doc.getBody();
const tblLoc = body.findText("\\$");
console.log(tblLoc.getElement().asText().getText())
Reference:
findText(searchPattern)

Google Sheets Script TextFinder FormulaText Doesn't Work with Named Ranges?

I am trying to create a function that modifies formulas in a range that changes bounds over time.
WORKS
var range=ss.getSheetByName('cleaned').getRange("A499:AH540");
sumForm=range.createTextFinder("ss2").matchFormulaText(true);
sumForm.replaceAllWith('ss');
DOESN'T WORK
var range=ss.getRangeByName('cleanedSums');
sumForm=range.createTextFinder("ss2").matchFormulaText(true);
sumForm.replaceAllWith('ss');
Yes, I realize the static defined range is slightly different than the named range definition, but the formulae in question appear in columns E, M, V, & AD.
I created a publicly editable version of the sheet: https://docs.google.com/spreadsheets/d/1v_rYBDz6NWmv5JFghsNl2ScAP3u0fq2QHEmZM5ArvG4/edit
The function in question is ss1(), accessible from UI menu Hide (Create SS1). Any insights would be appreciated.
I am not sure how to go about implementing this functionality without using a silly helper cell at the lower bound of the data, which would eventually become inaccurate or iterating over the rows to find the lower bound each time. Neither are particularly appealing and I am struggling to think of why the TextFinder replacement function would not work on named ranges. Sorry if this belongs on /webapps
Your code works as a separate function.
Most likely your global variables interfered with the script.
function myFunctionTest() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range=ss.getRangeByName('cleanedSums');
sumForm=range.createTextFinder("ss2").matchFormulaText(true);
sumForm.replaceAllWith('ss');
}

Google Script: Adding 1 to a value

I'm trying to add 1 to the value of a cell in a Google Spreadsheet when a certain event happens. But instead of...say changing -6 to -5, it changes it to -61. What's wrong with my code?
var Spending = sheet.getRange("B2");
var SPEND = [Spending.getCell(1, 1).getValue()];
**EVENT TRIGGERED**
SPEND = SPEND+1;
Spending.setValue(SPEND);
It appears that was reading the value as a string and simply concatenate it. Try to parse the the var SPEND to Integer.
example:
SPEND = parseInt(SPEND)+1 or SPEND = Number(SPEND)+1
for some reason i have yet to fully discover (mainly through lack on needing to know the deeper answer), + is treated as a concatenation for anything but an integer value. so unless the var is declared as an intiger, it's treated as a string. if it's a string, the "+" means to concat, not perform a mathematical operation. "-" doesn't seem to have the same dual usage, only being used for mathematical operations.
Hope that helps!

Understanding google spreadsheets set vs get logic

I'm desperately trying to learn to "think" in this language (my native coding language is VBA/ASP).
Here is a very basic example that simply reads the contents of a specific cell in Sheet1 and then assigns that value to a variable named rngVal
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Sheet1"));
var rngVal = ss.setActiveSelection("A1").getValues();
When I attempt to deconstruct this very simple 3-line piece of code (in the interest of understanding it's purpose), I admit to being rather perplexed.
Here's why:
When I insert a msgbox after each line, I get this:
var ss = SpreadsheetApp.getActiveSpreadsheet(); = "Spreadsheet"
ss.setActiveSheet(ss.getSheetByName("Sheet1")); = "Spreadsheet"
var rngVal = ss.setActiveSelection("A1").getValues(); = The cell value
What is "Spreadsheet"?
How is this helpful(?) and how can I use it for the benefit of navigating the worksheet and reading/writing values within it?
That's 2 lines of code that does what(?), and for what benefit?
(I'm not trying to be combative, I'm just trying to understand so that I can learn to "think" in this language)
Secondly:
In the first line, what am I get ting?
In the second line I'm first set 'ting, and then I'm get 'ting? (I'm very confused about what that line is actually doing)
In the third (last) line there is more set 'ting and get 'ting, although this time it's not nested.
This is very basic code (functionally), but I'm having trouble grasping it's logic in terms of how to "think" using that logic.
Is there anyone out there who would be kind enough to show some patience and help me by describing the step by step logic of this simple code of selecting a cell, capturing it's value, and then assigning it to a variable?
Please understand, that when I "think" of how to do that very simple task, I can do it in one very concise line (in VBA)...
rngVal = Sheets("Sheet1").Range("A1").Value
...therefore I am really confused by all of the "Spreadsheet" stuff and the need for 3 lines filled with set's and get's.
Anyone? Please?
Hmm somehow the correct answer (my other one) got voted down. Again its pointless to do setactiveselection, there is no need to change the selected cell it just makes your code slower.
Google uses Javascript Classes to represent these different Objects.
I've split line 3 into two parts to help with my explanation. Please compare:
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Sheet1"));
var rng = ss.setActiveSelection("A1");
var val = rng.getValues();
Each line above returns a different class of Object. The first line returns an Object with class Spreadsheet, the second line returns an object of class Sheet, the third line returns an object of class Range, and the final line returns an object of class Object.
Take a look here for documentation on the various actions (methods) that can be performed on/with the different classes of objects. When I'm building a Google App, I live on that webpage.
To compare the above with the code you understand from VBA, this line:
rngVal = Sheets("Sheet1").Range("A1").Value
Could be written in Javascript in one line as follows:
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").setActiveSelection("A1").getValues();
Hope that helps. Let me know if you have questions.
You guys are doing it more complex than neeeded. No need to mess with selection.
Getactivespreadsheet ().getsheetbyname ("x").getrange ("a1").getValue () also works.

Writing spreadsheet function via code

I want to write this into the range B1..
=split(A1," ")
(the delimiter is a space)
How do I do it using code?
I tried this...
var splitCell = "=SPLIT(A1," ")";
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B1").setFormula(splitCell);
But the quotes are obviously ruinous. Therefore how are quotes handled in such instances?
EDIT
Sorry. Brain-fart.
var splitCell = '=SPLIT(A8," ")';
I thought that I had already tried that (about a million times) but when I just tried it (again) it worked no problem.
Thanks anyway
(I'm sure I'll be back with less smelly questions in the future)
At the time that I asked my question, I was not allowed to answer my own question (newbies here have limitations). However the answer to my question was stated in the "edited" part of my question.
I apologize for the confusion, but at the time I had no other options available to me, so therefore my question remained "Unanswered" even though I already resolved it.
Anyway, the answer is:
var splitCell = '=SPLIT(A8," ")';