Reputation: 5
I am trying to write a simple 'If statement' that for some reason isn't able to properly find what data is in the cell it's referencing('Data' S26). It should contain either a 0 or a 1, but it doesn't seem to matter if I have a 0 or a 1 in the cell - either from a formula or directly, the following formula is always returning a "2". Completely new to this so probably something easy, but I can't seem to find the answer!
Thanks!
function InsertWinLoss(){
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
var gameSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Games');
var currentRow = gameSheet.getLastRow();
if (dataSheet.getRange('S26') =='1') {
gameSheet.getRange(currentRow,3).setValue('1');
} else if (dataSheet.getRange('S26') = '0') {
gameSheet.getRange(currentRow,3).setValue('0');
} else { gameSheet.getRange(currentRow,3).setValue('2') }
}
Upvotes: 0
Views: 83
Reputation: 36
Seems like the original post might have been answered in comments, but for what it's worth:
if
statementselse if
condition has a syntax error; the expression uses the assignment operator =
, rather than checking for equality ==
setValue()
calls are writing 1, 0, or 2 as Strings, rather than Numbers; omit the '' if this isn't intendedHere's my two cents on rewriting the function to be a little more compact! Hope it's useful.
//By convention, functions should start lowercase
function insertWinLoss() {
//Get Sheets
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
var gameSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Games');
//Assign value of your win/loss cell to new variable
var winLossValue = dataSheet.getRange('S26').getValue();
//Set your output range
var currentRow = gameSheet.getLastRow();
var outputRange = gameSheet.getRange(currentRow, 3);
//Now your if/else statements are a little more compact
//and it's easier to change values like the win/loss cell or output column!
if (winLossValue == 1) {
outputRange.setValue('1');
} else if (winLossValue == 0) {
outputRange.setValue('0');
} else {
outputRange.setValue('2')
}
}
Function should give desired result of writing 1, 0, or 2 to output range based on input to win/loss range.
Upvotes: 1