Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-formulamacro-recorder

Copy results of a query function from one worksheet and append it at the end of another worksheet in a diferent file


Is it possible to select the results of a QUERY (=query(Dom,"select * WHERE G is not null")) function from google sheet and append it at the end of another google sheet?

Error: "Cannot find method openById(string,string).

I basically have two Google Sheets in two different locations and they both are in a flat-file format.

function Dom() {
var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getSheetByName('Dom_Sum').showSheet()
  .activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Dom_Sum'), true);
  
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getCurrentCell().offset(1, 0).activate();
  var currentCell = spreadsheet.getCurrentCell();
  spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
  currentCell.activateAsCurrentCell();
  currentCell = spreadsheet.getCurrentCell();
  spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.NEXT).activate();
  currentCell.activateAsCurrentCell();
  currentCell = spreadsheet.getCurrentCell();
  spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.NEXT).activate();
  currentCell.activateAsCurrentCell();
  currentCell = spreadsheet.getCurrentCell();
  spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.NEXT).activate();
  currentCell.activateAsCurrentCell();
  currentCell = spreadsheet.getCurrentCell();
  spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.NEXT).activate();
  currentCell.activateAsCurrentCell();
  
  
 var source = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = source.getSheets()[0];
 var destination = SpreadsheetApp.openById("17J0_YiUANs9CZNmq2tqqvwjdAT2P8IVcx3NYljhqTFk","Master_Db!A1");
  spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
  spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
  spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
  spreadsheet.getCurrentCell().offset(1, 0).activate();
 sheet.copyTo(destination);
  
 
  spreadsheet.getSheetByName('Dom_Sum').hideSheet()
    
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getSheetByName('Indent Form_Dom').showSheet()
  .activate();
  spreadsheet.getRangeList(['C4:D9', 'A12:M112']).activate()
  .clear({contentsOnly: true, skipFilteredRows: true});
  spreadsheet.getRange('C4').activate();

  
};

Solution

  • You are providing two parameters to openById() when it only admits one.

    Also, if I understand you correctly, you just want to copy the (non null) data from column G to another sheet.

    If that is so, you can try this:

    function Dom() {
      var origin_sheet = SpreadsheetApp.getActive().getSheetByName('Dom_Sum');
      var firstRow = 1;
      var firstCol = 7;
      var numRows = origin_sheet.getLastRow();
      var numCols = 1;
      var origin_values = origin_sheet.getRange(firstRow, firstCol, numRows, numCols).getValues();
      var dest_values = [];
      for(var i = 0; i < origin_values.length; i++) {
        if(origin_values[i][0] != '') {
          dest_values.push(origin_values[i]);
        }
      }
      var dest_id = "1ZGq7L7bvF1INuDgZxhHnVsihkYkYubmncSAE5uC-Pq4";
      var dest_sheet = SpreadsheetApp.openById(dest_id).getSheetByName("Master_Db");
      var numRowsDest = dest_values.length;
      var dest_range = dest_sheet.getRange(1, 1, numRowsDest, 1);
      dest_range.setValues(dest_values);
    }
    

    If this is not what you wanted I'm sorry.