Pause ad groups based on sheet

160 views
Skip to first unread message

Sarah Jones

unread,
Feb 5, 2020, 10:01:22 AM2/5/20
to Google Ads Scripts Forum

Is there a way to create a script that links to a sheet with all campaigns and ad groups in, and then if I put the status as 'paused' for a specific ad group, the script will then run and pause the ad group? Or vice versa.

Google Ads Scripts Forum Advisor

unread,
Feb 5, 2020, 2:59:44 PM2/5/20
to adwords-scripts+apn2wqeab0-1bf72...@googlegroups.com, adwords-scripts+apn2wqeab0-1bf72...@googlegroups.co, adwords...@googlegroups.com
Hi Sarah,

Yes, this is possible. Depending on how many rows you have in your sheet, you could simply iterate other each row, pausing or enabling each ad group. For example, using a three column setup with campaign name, ad group name and status, the following script could be a good starting point:

var SPREADSHEET_URL = "YOUR_SPREADSHEET_URL";

function main() {
  var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);  
  
  var data = spreadsheet.getRange("A2:C").getValues(); //Assuming headers and a three column setup 
    
  for(var i = 0; i < data.length; i++) {
    var row = data[i];
    if(row[0] == '')
      break;
    else {
      try {
        var status = row[2];
        var ag = row[1];
        var campaign = row[0];
        var adGroup = AdsApp.adGroups().withCondition("Name = '" + ag + " '").withCondition("CampaignName = '" + campaign + " '").get().next();

        if(status == "PAUSE") //Depending on what value you want to use in your status column
          adGroup.pause();
        else
          adGroup.enable();
      }
      catch(e) { Logger.log(e + "-- Campaign: %s and ad group: %s do not exist", row[0], row[1])}
    }
  }  
}

You can find more useful code snippets on working with spreadsheets here.

Regards,
Matt
Google Ads Scripts Team

ref:_00D1U1174p._5001UV02Gj:ref

Elena Morgan

unread,
Jun 30, 2020, 12:57:18 PM6/30/20
to Google Ads Scripts Forum
Hi Matt,

Thanks for this.

Could I ask - is it possible for this script to reference one 3 column tab (as suggested) on a Google Sheet that has multiple tabs?
Secondly, my 3 columns are populated via formulae so aren't "values" - will this still work?

Thanks,
Ellie

Google Ads Scripts Forum Advisor

unread,
Jun 30, 2020, 4:14:16 PM6/30/20
to adwords-scripts+apn2wqdmejuvoym0...@googlegroups.com, adwords...@googlegroups.com
Hi Elena,

To specify a certain spreadsheet by name, please make the changes in bold:
  var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getSheetByName("TAB_NAME");
  var data = spreadsheet.getRange("A2:C").getValues(); //Assuming headers and a three column setup 

The script will still work with formulas.
Reply all
Reply to author
Forward
0 new messages