Reputation: 17
function copyRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow() - 1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 100);
dataRange.activate();
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[1]; // First Column
var title = row[6];
if (title.contains("SVP" || "VP" || "Director" || "director" || "CEO" || "Founder" || "owner")) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var destination = ss.getSheetByName("Director's, VP's, CEOs, SVP's - MANUAL");
var range = destination.getRange("A2:D1000");
row.copyValuesToRange(startRow, 1, numRows, 100);
}
}
}
Here's a screenshot of the spreadsheet:
Not sure what I'm missing, but the code doesn't seem to be working... Saying that it's having an error:
Cannot find function contains in object Associate Account Strategist.
Upvotes: 1
Views: 264
Reputation: 45750
The error message isn't lying to you.
Cannot find function contains in object Associate Account Strategist. ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The error message would have come with a line number as well, which would point you to the place that the interpreter determined there was an error. Probably this line:
if (title.contains("SVP" || "VP" || "Director" || "director" || "CEO" || "Founder" || "owner")) {
That is valid syntax in JavaScript and Google Apps Script, but it's not doing what you think it is.
The error message says you have an object containing "Associate Account Strategist" - at this point in your code that would be a string from row[6]
. You're trying to use the String.contains()
method. This is fairly new, introduced in ECMA 6 - but is not supported (yet) by Google Apps Script. That's why the function named contains
is not found.
Options are described in How to check whether a string contains a substring in JavaScript?
The way you're trying to use contains
isn't right, either. Your expectation is probably that you'll find out whether title
contains "SVP", or "VP", or etc. Instead, what will happen is that all the strings will be OR
'd together, which will evaluate to true
. Then you'd be left with if (title.contains(true))...
, which will always evaluate to the same thing, no matter what string title
is. You would need to make each comparison separately, perhaps in a loop through an array to reduce code.
Upvotes: 1