New user. My first question.
I'm retired now, but I used to work with Excel and VBA a little. Now, I'm trying to help out a friend with a Google Sheets application.
My task is pretty simple. Copy a range of data from one sheet and go to another sheet, go to the first empty row, and paste the data as values.
First of all, the variable UHsheet appears in pink text on my Apps Script screen, but the variables oldRange and newRange do not, they are both greyed out. Not sure why that is, but I haven't been able to figure it out, and I'm thinking that might be my main issue.
Anyway, when I run this script, I get this error message:
TypeError: oldRange.copyTo is not a function.
Is this because Apps Script is not recognizing the oldRange variable? If so, how do I fix it?
Thanks.
/** @OnlyCurrentDoc */
function SaveHistory() {
var oldRange = SpreadsheetApp.getActiveSheet().getSelection();
var UHsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Usage History');
var newRange = (UHsheet.getLastRow() 1);
oldRange.copyTo(newRange, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
CodePudding user response:
function SaveHistory() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const rg = sh.getActiveRange();
const uHsheet = ss.getSheetByName('Usage History');
rg.copyTo(uHsheet.getRange(uHsheet.getLastRow() 1, 1), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}