简体   繁体   中英

Copy/Pasting a Range of Values into the Next Empty Set of Cells on a Separate Sheet

I am attempting to take data from a "worksheet" which will always begin as the active sheet into a seperate sheet that is determined by the month of the year. That has been no problem so far and my code does function in this way. However I can not understand how to take that predetermined amount of info "A9:AG11" in our static worksheet and paste into the next empty set of rows on our shared monthly sheet. Here is my current code that does work, ui button, and all. But it will of course only write over any data already existing on the second sheet. Any help would be appreciated!

function BP3CallTab() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.alert(
     'Please confirm',
     'Ready to Submit 3-Call Audit?',
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
    copyFunction ();
  }

    function copyFunction () {
      var inputRange =  SpreadsheetApp.getActiveSpreadsheet().getRange("A9:AG11");
      var inputValues = inputRange.getValues();
      var shname = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dash").getRange("Q4").getValue()  
      var emptyrow = SpreadsheetApp.getActive();
       emptyrow.getRange('A17').activate();
       emptyrow.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
       emptyrow.getCurrentCell().offset(1, 0).activate();
      var outputRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getRange("A17:AG19").setValues(inputValues);
    }

    ui.alert('Audit Complete!✅');

  if (result ==ui.Button.NO) {
    // User clicked "No" or X in the title bar.
    ui.alert('No Changes Made.');
  }
};

You need to use a different method to define the output range.

  • Try getRange(row, column, numRows, numColumns) .

  • "row" is obtained by using getLastRow() , say...

    • var targetLR = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getLastRow()
  • "Column" =1 (to denote column A)

  • "numRows" = 3 (the number of rows between "A17:A19")

  • "numColumns" = 33 (the number of columns between "A" and "AG")

So your output range would be something like:

  • var outputRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getRange(targetLR,1,2,33).setValues(inputValues)

A slight variation of @Tedinoz's approach so that (a) the destination range is always exactly the same as the size of data being copied, otherwise it results to an error and (b) the checking of the next empty row is still correct even if a cell somewhere down below was accidentally edited --- something that affects getLastRow()

function copyFunction () {
  var inputRange =  SpreadsheetApp.getActiveSpreadsheet().getRange("A9:AG11");
  var inputValues = inputRange.getValues();
  var inputValuesRows = inputValues.length;     // no. of rows of copied data
  var inputValuesCols = inputValues[0].length;  // no. of columns of copied data

  //get your destination sheet and its first empty row
  var sharedSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Shared Monthly Sheet");
  var firstEmptyRow = getFirstEmptyWholeRow("Shared Monthly Sheet");
  Logger.log(firstEmptyRow);

  //copy your data into your destination starting from firstEmptyRow and column A
  sharedSheet.getRange(firstEmptyRow,1,inputValuesRows,inputValuesCols).setValues(inputValues);
}

/*
 * Adapted from Mogsdad's "whole row" checker 
 * from https://stackoverflow.com/questions/6882104/faster-way-to-find-the-first-empty-row-in-a-google-sheet-column
 */
function getFirstEmptyWholeRow(sheetname) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
  var range = sheet.getDataRange();
  var values = range.getValues();
  var row = 0;
  for (var row=0; row<values.length; row++) {
    if (!values[row].join("")) break;
  }
  return (row+1);
}

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