How can I store a range of cells to an array?

后端 未结 4 1373
孤独总比滥情好
孤独总比滥情好 2021-01-06 16:55

If I have a list of data in cells A1:A150 (but the amount can vary), is there a way to push that into an array without looking at each cell individually to determine if it i

4条回答
  •  不要未来只要你来
    2021-01-06 17:30

    Try this:

    It will allow you to select any column on the sheet.

    var ui = SpreadsheetApp.getUi();
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    
    function onOpen() {
    
      ui.createMenu('Sheet Functions')
     .addItem('Get values from column', 'getVals')
     .addToUi();    
     }
    
    function getVals() {
    var sheet = ss.getActiveSheet();
    var getColumnLetter = ui.prompt('Select column..' , 'Enter the letter of the target column..', ui.ButtonSet.OK_CANCEL);
      if(getColumnLetter.getSelectedButton() == ui.Button.CANCEL) { 
      return } else {
      getColumnLetter = getColumnLetter.getResponseText().toUpperCase();
      }
    
    var columnNo = getColumnLetter.charCodeAt(0) - 64;
    
    try { var data = sheet.getRange(1, columnNo, sheet.getMaxRows()).getValues().filter(String); } catch (e) { ui.alert('Invalid input please try again.', ui.ButtonSet.OK); return;}
    
    /*
    *
    * Do what ever you need to do down here
    *
    */
    }
    

提交回复
热议问题