Home > OS >  Google Sheets - Automatically record values from Sheet 1 to Sheet 2 on a specific day and time of th
Google Sheets - Automatically record values from Sheet 1 to Sheet 2 on a specific day and time of th

Time:06-06

I have a form that gathers the amount hours each member of a team has dedicated to a specific task on one sheet, and it changes every week. These values are deleted every Monday I need to create a record of these values on a new sheet every Sunday, to keep a record. I have a code that would take the values from a cell and paste it in the first available row in that same sheet, But I need it to paste it in a different sheet.

The code I have is this:

function recordValue() {
   var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
    var date = new Date();
    var value =sheet.getRange("K6").getValue();
    sheet.appendRow([date, value]);
}

Any help would be appreciated.Thanks in advance!

CodePudding user response:

Try

function recordValue() {
   var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
   var date = new Date();
   var value =sheet.getRange("K6").getValue();
   SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OtherSheetName").appendRow([date, value]);
}

or

function recordValue() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var values = [new Date()]
  sheet.getRange("K6:K10").getValues().forEach(v => values.push(v[0]));
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OtherSheetName").appendRow(values);
}

or

function recordValue() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OtherSheetName");
  var range = "K6:K10";
  dest.getRange(range).setValues(sheet.getRange(range).getValues());
}
  • Related