Reputation: 159
I am new to using Google App Script and I don't understand fully how the "Logger.log" command works.
I'm working with Google App Script as a bound script to a spreadsheet. Below is a snipet of the code I am working on:
function tasks() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Task List");
sheet.activate();
var hi = 23;
Logger.log(hi);
Logger.log("The range is " + sheet.getRange(1, 1, 10, 1));
}
When I run the code, this is displayed in the logger:
[16-06-02 00:56:12:129 EDT] 23.0
[16-06-02 00:56:12:130 EDT] The range is Range
Why is it displaying "Range" for the sheet.getRange function rather than the array of data.
Also can someone please explain the difference between "getRange" and "getDataRange".
Upvotes: 3
Views: 8172
Reputation: 2286
You are logging a range object and as such the logger will tell you that it is just that. The documentation for google apps scripting is very good. In particular, for your case you would have to be looking at the what getRange() returns
So if you want to log what the range actually is, you can use getA1Notation() which will tell you the range. In this case since it's Starts at row 1 column 1 and is 1 row and 10 columns in length it will be A1:J1. If you wish to see what values are in that particular range you could use getValues() which will return a 2D array.
Logger will log the strings and numbers pretty much as you would expect and arrays would also be output in a string format
Upvotes: 3
Reputation:
The Range object is a pointer at a range, like A1:A10. This is different from a set of values in a range, which is an array of entries. To get the values from a range, use the getValues
method:
Logger.log("The values are " + sheet.getRange(1, 1, 10, 1).getValues());
To your second question: the documentation is pretty clear on what getDataRange does: it gives you the range that begins with A1 and stretches as far as there is data in the sheet. If you only put some data in B5 and D2, and nowhere else, the DataRange is A1:D5. With getRange
you specify which range you want.
Upvotes: 5