Page Summary
-
The first code snippet demonstrates how to retrieve a negative keyword list by its name using
AdsApp.negativeKeywordLists(). -
The second code snippet shows how to associate an existing negative keyword list with a specific campaign.
-
The third code snippet illustrates how to remove all shared negative keywords from a given negative keyword list.
Get a negative keyword list by name
function getNegativeKeywordList ( name ) { const negativeKeywordLists = AdsApp . negativeKeywordLists () . withCondition ( ` shared_set . name = "${name}" ` ) . get (); if ( ! negativeKeywordLists . hasNext ()) { throw new Error ( ` Cannot find negative keyword list with name "${name}" ` ); } return negativeKeywordLists . next (); }
Construct a new negative keyword list and add it to a campaign
function addNegativeKeywordListToCampaign ( campaignName , negativeKeywordListName ) { const negativeKeywordLists = AdsApp . negativeKeywordLists () . withCondition ( ` shared_set . name = "${negativeKeywordListName}" ` ) . get (); if ( ! negativeKeywordLists . hasNext ()) { throw new Error ( ` Cannot find negative keyword list with name "${negativeKeywordListName}" ` ); } const negativeKeywordList = negativeKeywordLists . next (); const campaigns = AdsApp . campaigns () . withCondition ( ` campaign . name = "${campaignName}" ` ) . get (); if ( ! campaigns . hasNext ()) { throw new Error ( ` Cannot find campaign with the name "${campaignName}" ` ); } const campaign = campaigns . next (); campaign . addNegativeKeywordList ( negativeKeywordList ); }
Remove all the shared negative keywords in an negative keyword list
function removeAllNegativeKeywordsFromList ( name ) { const negativeKeywordLists = AdsApp . negativeKeywordLists () . withCondition ( ` shared_set . name = "${name}" ` ) . get (); if ( ! negativeKeywordLists . hasNext ()) { throw new Error ( ` Cannot find negative keyword list with name "${name}" ` ); } const negativeKeywordList = negativeKeywordLists . next (); for ( const negativeKeyword of negativeKeywordList . negativeKeywords ()) { negativeKeyword . remove (); } }

