Search code examples
google-apps-scriptgoogle-sheetsspreadsheetgoogle-appsgoogle-sheets-api

Check row if value exists


if (purchaseSheet && range.getColumn() == purchasedColumn && ["Yes", "No", "Repeat"].indexOf(range.getValue()) > -1) {
    if (targetSheet.getRange("A:A").getValue() != taskID) {
        sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).copyTo(targetRange); //- moveTo
    }
}

My second if statement doesn't seem to check the range to see if a row contains the same taskID.

How do I check "Column A" to see if a value exists or not in a row?


Solution

  • If you get a range that is more than a cell and call getValue() on it it will only get one value, the one in the top left cell of the range.

    What you want to do is

    targetSheet
        .getRange("A:A")
        .getValues()
        .map(function(row) {return row[0];})
        .indexOf(taskID) > -1
    

    if you want it to be found or === -1 if you want it not to exist