简体   繁体   中英

How to take a single cell and a range in a custom function and take the range data in an array in google sheets

Firstly, I am very new here. Searched the whole internet about this and finally landing a question here.

 function StockCoverW(stock,range){

     var x = stock;
     var r = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
     var r1 = SpreadsheetApp.getActiveSheet().getNamedRanges(range);
    }

Here, stock is a single cell for example: A1 and range is a range of cell for example: A2:E2

so the function call will be StockCoverW(A1,A2:E2) now I need to take the values from the range cells into an Array.

Obviously the code here is not correct as cannot debug or do anything.

How to implement this in google spreadsheet script?

Change getDataRange (which grabs all valued on sheet) to getRange(range)

function StockCoverW(stock,range){
     var x = stock;
     var r = SpreadsheetApp.getActiveSheet().getRange(range).getValues();
     var r1 = SpreadsheetApp.getActiveSheet().getNamedRanges(range);
}

Note though that the function doesn't return or alter anything so when it runs it just gets the valued and the named ranges.

Also note that the array you get with values is a 2d array and if you want it to be just a simple array then use

function StockCoverW(stock,range){
     var x = stock;
     var r = SpreadsheetApp.getActiveSheet().getRange(range).getValues()[0];
     var r1 = SpreadsheetApp.getActiveSheet().getNamedRanges(range);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM