mercredi 22 juillet 2020

GAS/JavaScript: How do I return result and not both

I have this code that supposed to process a condition before appending to a sheet.

I would like to only append one value, i.e. if the data has two matches only one should be appended to the sheet and the other ignored and the loop continues to the next item.

Here is the code that appends both if they meet the criteria. I used the || operator.

    if (contents.length > 0) {
        var dstSheet = SpreadsheetApp.getActive().getSheetByName('Projects');
        var values = contents.map(e => ([e.email, e.role]));
        var new_values = values.filter(item => {
                                       
            if (item[1] === 'Project Leader' || item[1] === 'Senior Project Leader') {
                return true
            }

        })
       dstSheet.getRange(3, 8, new_values.length, new_values[0].length).setValues(new_values); 
    }
}

Here is the one I have worked on, but it still passes two values. Essentially, I would like either Project leader or Senior Project Leader to be appended and not both.

    if (contents.length > 0) {
        var dstSheet = SpreadsheetApp.getActive().getSheetByName('Projects');
        var values = contents.map(e => ([e.email, e.role]));
        var new_values = values.filter(item => {
                                       
            if ((item[1] === 'Project Leader' && item[1] != 'Senior Project Leader') || (item[1] === 'Senior Project Leader'&& item[1] != 'Project Leader')) {
                return true
            }

        })
       dstSheet.getRange(3, 8, new_values.length, new_values[0].length).setValues(new_values); 
    }
}

Aucun commentaire:

Enregistrer un commentaire