Home » Javascript » Spreadsheet tracker

Spreadsheet tracker

Posted by: admin October 18, 2018 Leave a comment


Im having trouble with an script that posts the outcome twice, I have reviewed the script but I cant find the issue.

The script gets the "Timestamp", "Cell address", "Column label" and "Value entered" and post it on the sheet named "Tracker" but it gets posted twice

function onEdit() {
  var sheetsToWatch = ['Responses'];
  // name of the sheet where the changelog is stored
  var changelogSheetName = "Tracker";

  var timestamp = new Date();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getActiveCell();
  var sheetName = sheet.getName();

  // if it is the changelog sheet that is being edited, do not record the change
  if (sheetName == changelogSheetName) return;

  // if the sheet name does not appear in sheetsToWatch, do not record the change
  var matchFound = false;
  for (var i = 0; i < sheetsToWatch.length; i++) {
    if (sheetName.match(sheetsToWatch[i])) matchFound = true;
  if (!matchFound) return;

  var columnLabel = sheet.getRange(/* row 1 */ 1, cell.getColumn()).getValue();
  var rowLabel = sheet.getRange(cell.getRow(), /* column A */ 1).getValue();

  var changelogSheet = ss.getSheetByName(changelogSheetName);
  if (!changelogSheet) {
    // no changelog sheet found, create it as the last sheet in the spreadsheet
    changelogSheet = ss.insertSheet(changelogSheetName, ss.getNumSheets());
    // Utilities.sleep(2000); // give time for the new sheet to render before going back
    // ss.setActiveSheet(sheet);
    changelogSheet.appendRow(["Timestamp", "Cell address", "Column label", "Value entered"]);
  changelogSheet.appendRow([timestamp, cell.getA1Notation(), columnLabel, cell.getValue()]);