Hi,
Thank you for reaching out to the Google Ads Scripts support team.
@Nils, Thank you for your input.![]() |
Google Ads Scripts Team |
[2025-07-10 11:41:47Z GMT] This message is in relation to case "ref:!00D1U01174p.!500Ht01rhi8x:ref" (ADR-00318201)
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scrip...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/adwords-scripts/95f21413-ef23-43fb-9bd9-08f3f363e1fen%40googlegroups.com.
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scrip...@googlegroups.com.
Hi Kasia,
I would like to inform you that the method you are using to push key values to the “excludedAlready” object is causing this error. To resolve this error using the “new set ()” property instead of an empty object. Also, using ‘try’ and ‘cache’ methods will be effective. I would recommend that you use the below code.
function main() {
const campaignName = "32154 SPM US CARTIHEAL TRAFFIC New Structure 2025";
const termsToExclude = ["elderly", "foods", "supplement", "remedies", "seniors", "best", "failure", "surgery cost", "top knee surgeons", "tips", "timelines", "problems after", "at 14 weeks", "creams", "bone on bone", "side effects", "in my area", "old age", "why does it take so long", "do's and don ts", "near me", "at age", "at home", "at 70"];
const matchTypes = ["broad", "phrase", "exact"];
const campaignIterator = AdsApp.campaigns()
.withCondition(`Name = '${campaignName}'`)
.get();
if (!campaignIterator.hasNext()) {
Logger.log("Campaign not found: " + campaignName);
return;
}
const campaign = campaignIterator.next();
const excludedAlready = new Set();
for (let i = 0; i < termsToExclude.length; i++) {
const term = termsToExclude[i].toLowerCase();
for (let j = 0; j < matchTypes.length; j++) {
const matchType = matchTypes[j];
let formattedKeyword = "";
switch (matchType) {
case "broad":
formattedKeyword = term;
break;
case "phrase":
formattedKeyword = `"${term}"`;
break;
case "exact":
formattedKeyword = `[${term}]`;
break;
default:
Logger.log(`Invalid match type encountered: ${matchType}. Skipping.`);
continue;
}
const key = formattedKeyword;
if (!excludedAlready.has(key)) {
try {
campaign.createNegativeKeyword(formattedKeyword);
Logger.log(`Added negative keyword: ${formattedKeyword} (Match Type: ${matchType})`);
excludedAlready.add(key);
} catch (e) {
Logger.log(`Error adding negative keyword '${formattedKeyword}' for campaign '${campaignName}': ${e.message}`);
}
} else {
Logger.log(`Skipping duplicate negative keyword: ${formattedKeyword} (Match Type: ${matchType})`);
}
}
}
}
I hope this helps! Kindly get back to us if you still face any errors even after using the modified code.
![]() |
Google Ads Scripts Team |
[2025-07-11 08:48:34Z GMT] This message is in relation to case "ref:!00D1U01174p.!500Ht01rhi8x:ref" (ADR-00318201)