Calvin Gaunce Calvin Gaunce - 1 year ago 106
Javascript Question

Returning 'Range' instead of actual string in Google Apps Script

I am using the following function to return a concatenated string in Google Sheets. In this instance, B4 equals 'cardiology' and B1 equals '82-01'.

The expected result is


However, I am getting this result,

Why is the function returning 'Range' instead of the actual strings?

function highLevelData() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // get the spreadsheet object
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[1]); // set the first sheet as active
var sheet = spreadsheet.getActiveSheet(); // get the active sheet
var reportConfig = spreadsheet.getSheets()[0]; // set report configuration sheet
var targetSpecialty = sheet.getRange("B4"); // get target specialty
var specialtyDimension = "dimension2=~"; // set the sytanx string for the specialty dimension
var targetCase = sheet.getRange("B1"); // get case
var eventCategoryDimension = ";ga:eventCategory=~MS"; // set the syntax string for the event category dimension
var filterOutput = specialtyDimension + targetSpecialty + eventCategoryDimension + targetCase; // concatenate full filter string
var filterCell = reportConfig.getRange("B11"); // set the filter cell

filterCell.setValue(filterOutput); // output filter string to filter cell


Answer Source

The "getRange()" functions return a Range object which represents cell(s) in the sheet.

To get the contents of the cell, call ".getValue()" on the Range object.

var targetSpecialty = sheet.getRange("B4").getValue();


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download