Wrenches
loading...

Google Sheets Complete Clan Logs Guide

The Clan Logs Sheet contains 100 lines in total, combined of the latest joins, leavings, promotions, demotions and clan member name changes. But if you'd like to be able to keep a log with older data then the 100 latest then this is the right guide for you!

In your google sheet, look at the bottom left corner and click the add new sheet button.



Then click the arrow pointing down on your newly created sheet and choose Rename...



Name it "Complete Log" (without the quotes).



Click on row #2, scroll to the bottom of the sheet. Then hold the Shift key and click the very last row which should be #1000.



Right click anywhere in the row number column and select "Delete rows 2 - 1000".



Optional - You can do the same for column C - Z.

Now go to the menu bar at the top and click Tools then Script Editor.



The Script Editor will open and you'll see this.



Highlight everything in there and replace it with the following:

//////////\\\\\\\\\\
// Custom Scripts \\
//  For Clan_Bot  \\
//   By Cabola    \\
//////////\\\\\\\\\\

// custom menu
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('█╣ CLAN_BOT c[○┬●]כ MENU ╠█')
  .addItem('Update Complete Log','updateLogs')
  .addToUi();
}

// Update Complete Log Sheet
function updateLogs(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sLogs = ss.getSheetByName("Logs");
  var sCLogs = ss.getSheetByName("Complete Log");
  var ui = SpreadsheetApp.getUi();
  
  if(sLogs && sCLogs) {
    var lastRow = sLogs.getLastRow();
    var arrayNew = sLogs.getRange('A2:B' + lastRow).getValues();
    var arrayOld = sCLogs.getRange('A2:B' + lastRow).getValues();
    
    for (var i = 0; i < arrayNew.length; i++) {
      for (var j = 0; j < arrayOld.length; j++) {
      //Logger.log(arrayNew[i][0]+' == '+arrayOld[j][0]+' && '+arrayNew[i][1]+' == '+arrayOld[j][1]);
        if (arrayNew[i][0] == arrayOld[j][0] && arrayNew[i][1] == arrayOld[j][1]) {
          arrayNew.splice(i, 1);
          i--; //account for the splice
          break; //go to next i iteration of loop
        }
      }
    }
    // If no lines are found stop the script
    if (arrayNew.length == 0) { return; }
    // Count array num & add rows to beginning
    sCLogs.insertRowsBefore(2, arrayNew.length);
    // add values from array into the rows
    sCLogs.getRange(2, 1, arrayNew.length, arrayNew[0].length).setValues(arrayNew);
  } else {
    if(sLogs && !sCLogs) {
      ui.alert('Sheet Missing!', 'You\'re missing the "Complete Log" sheet. It will be created for you!', ui.ButtonSet.OK);
      SpreadsheetApp.getActiveSpreadsheet().insertSheet('Complete Log').getRange(1,1,1,2).setValues([["Date","Log"]]);
    } else if(!sLogs && sCLogs) {
      ui.alert('Sheet Missing!', 'You\'re missing the "Logs" sheet. It will be created for you!', ui.ButtonSet.OK);
      SpreadsheetApp.getActiveSpreadsheet().insertSheet('Logs');
    } else {
      ui.alert('Sheets Missing!', 'You\'re missing both the "Logs" and the "Complete Log" sheets. They will be created for you!', ui.ButtonSet.OK);
      SpreadsheetApp.getActiveSpreadsheet().insertSheet('Logs');
      SpreadsheetApp.getActiveSpreadsheet().insertSheet('Complete Log').getRange(1,1,1,2).setValues([["Date","Log"]]);
    }
  }
}

Should look like this:



Hit the save button so the Code.gs script file will be saved.



Select the updateLogs function just pasted in.



Now we'll test and see if everything is working so hit the Run button.



A popup will appear asking you to grant permission to access your data on google. This is because you're trying to run a custom script which wasn't made by google them self. So they're just making sure you know what you're doing. Click Review Permission to continue.



Now choose your google account and sign in to it.

Another warning is shown, click Advanced to expand the message downwards, and then click to go to the app.



You'll now see the screen where you actually allow the script to be executed via your google account. So do that click Allow.



The updateLog function will now execute and compare the data in the Logs sheet with the data in the Complete Log sheet. Since the Complete Log sheet is empty it'll just copy all 100 lines from the Logs sheet and that's that.
In the future it'll compare the first 100 rows in the Complete Log sheet with the Logs sheet and then copy the rows missing, into the top of Complete Log.

Futher more you can now update the Complete Log sheet by using a new menu located next to the Help menu in the sheet, called Custom Menu. This menu contains a single menu item which is called "Update Complete Log". The menu won't show up till next time you load the sheet, so just refresh the sheet (F5).



And you're done!