Home > Enterprise >  Copy & paste a range into selected range in Apps Script
Copy & paste a range into selected range in Apps Script

Time:09-08

enter image description hereCan't find a solution for my task. In google spreedsheets Copy range Constant Range E30:Q30 and paste into current cell but in selected array for example E33:Q132 (any string, E44:Q44 or E42:Q42 etc) So if I select different array to paste for ex. E12:Q12 it will show msg box "Try another cell" I understand that it is simple but I`m new in JS
Thank you for your help

Added script

function CopyPaste2() { 
  var spreadsheet = SpreadsheetApp.getActive(); 
  //spreadsheet.getCurrentCell().offset(0, 0, 1, 13).activate(); 
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Copy of StopWatch'), true); 
  spreadsheet.getRange('Copy of StopWatch!E30:Q30').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false); 
}; 

CodePudding user response:

Here is an example of how I would copy values from E30:Q30 to any row E33:Q. Since your script uses PASTE_VALUES there is no need to use copy but simply setValues().

And I assume you are copying to the same sheet.

function runTest() {
  let cell = SpreadsheetApp.getCurrentCell();  // any cell in the range E33:Q
  let sheet = cell.getSheet();
  if( sheet.getName() === "Sheet1" ) {
    if( cell.getRow() > 32 ) {  // row 33 and up
      if( ( cell.getColumn() > 4 ) && ( cell.getColumn() < 18 ) ) {  // columns E to Q
        let values = sheet.getRange("E30:Q30").getValues();
        sheet.getRange(cell.getRow(),5,1,13).setValues(values);
        return;
      }
    }
    SpreadsheetApp.getUi().alert("Try another cell");
  }
}
  • Related