Home > front end >  copyTo in AppScript activeRange.offset
copyTo in AppScript activeRange.offset

Time:01-25

I'm very new to Javascript and Apps Script I'm looking to create a function that updates another sheet based on the date in a specific range of the active sheet. I run and there are no errors but it is not transferring the values from the active sheet to the named sheet "2022 YTD".

Please help me see what I'm not seeing.

function updateYTD3() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("January");
  if (sheet) {
    sheet.getRange("A2:D32").copyTo(ss.getSheetByName("Feb").getRange("C2"),{contentsOnly:true});
  }
}

PICT1

PITC2

CodePudding user response:

It seems like that you are trying to copy data from the "January" sheet to the "Feb" sheet within the same spreadsheet, using the copyTo() method.

To troubleshoot this issue, you should first check if the sheet "Feb" exists within the spreadsheet. If it does not exist, the code will not be able to locate it, and the data will not be copied. To resolve this, you can add a check for the sheet's existence before calling the copyTo() method.

Additionally, you should check if the range in the target sheet is valid. If the range is not valid, the data will not be copied. Another factor to consider is whether the range you are trying to copy from ("A2:D32") contains any data. If it does not contain any data, there will be nothing to copy.

function updateYTD3() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("January");
  const targetSheet = ss.getSheetByName("Feb");
  if (sheet && targetSheet) {
    if(sheet.getRange("A2:D32").getValues().length > 0){
      sheet.getRange("A2:D32").copyTo(targetSheet.getRange("C2"),{contentsOnly:true});
    }
  }
}

CodePudding user response:

youtube.com/shorts/FNLQkIMMp4o?feature=shareVIdeo youtube

function same, sample in vba excel, so you can use the same function in google sheet

  • Related