今回はGoogle広告スクリプトを用いて除外キーワードリストの入稿を自動化する方法を紹介します。
作成した背景
広告出稿をしていると除外キーワードの入稿が定期的に発生します。
例えば、社内・競合他社・クラアイントから特定の語句を除外するよう頼まれることがあるのではないでしょうか。
その際、Google広告・Yahoo!広告(場合によってはMicrosoft広告)の管理画面に都度ログインして入稿しなければなりません。
各管理画面にログインするのは短い時間かもしれませんが、塵も積もれば山となります。
そこで、スプレッドシートに除外したいキーワードを記載して、それをもとに各媒体で除外を行う仕組みを構築することで効率化を図れるのではないかと考えました。
今回はGoogle広告をベースに自動化する方法を解説していきます。
今回は除外キーワードリストへの入稿を想定しています。(キャンペーン単位での入稿は想定していません。)
Yahoo!広告スクリプト版は以下の記事で紹介しています。

事前準備
- スプレッドシート内に3つのシートを作成
 - 
- 除外キーワードリスト
 - NegativeKeyword:アカウントに登録されている除外キーワード
 - add:これから入稿する除外キーワード
 
 - 除外キーワードリストに項目名を記載
 - 
Negative keyword・Match type・Negative keyword list name・有無判定・Action・Keyword or list

マッチタイプ(B列)・キーワードリスト名(C列)は入力規則を設定しておくと便利です。
有無判定(D列)には入稿有無を判定する関数を設定しています。
=if(or(A2="",B2="",C2=""),"", COUNTIFS(negativeKeywords!A:A,C2,negativeKeywords!B:B,A2,negativeKeywords!C:C,substitute(B2," MATCH","")))Action(E列)にはA:C列が入力されたらAddと表示される関数を設定しています。
=if(or(A2="",B2="",C2=""),"","Add")Keyword or list(F列)にはA:C列が入力されたらKeywordと表示される関数を設定しています。
=if(or(A2="",B2="",C2=""),"","Keyword")Match Type(G列)にはA:C列が入力されたら入稿に必要なマッチタイプの形式が表示される関数を設定しています。
=if(or(A2="",B2="",C2=""),"",concat(B2," MATCH")) - addシートに関数を設定
 - 
A1セルには以下の関数を入れています。
query関数を用いることで現在入稿されていない除外キーワードを抽出することが可能です。=IFERROR(query('除外キーワードリスト'!A:F,"select E, C, A, F, B where D = 0",1),"") 
サンプルコード
// スプレッドシートを指定
var spreadsheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/スプレッドシートID/edit#gid=0');
// シートを指定
var sheet_negativeKeywords = spreadsheet.getSheetByName('negativeKeywords');
// シートを指定
var sheet_add = spreadsheet.getSheetByName('add');
function main () {
  getNegativeKeywords();
  Utilities.sleep(5000);
  addNegativeKeywords();
  Utilities.sleep(5000);
  getNegativeKeywords();
  
}
function getNegativeKeywords () {
  
  // 除外キーワードリストのキーワード一覧を取得
  var report = AdsApp.report(
      "SELECT shared_set.name, shared_criterion.keyword.text, shared_criterion.keyword.match_type FROM shared_criterion WHERE shared_criterion.type = 'KEYWORD'"
  );
  // レポートの結果をスプレッドシートに書き込む
  report.exportToSheet(sheet_negativeKeywords);
}
function addNegativeKeywords () {
  // The format of this spreadsheet should match a valid bulk upload template.
  // See https://developers.google.com/google-ads/scripts/docs/features/bulk-upload
  // for the list of supported bulk upload templates.
  // bulkupload
  const upload = AdsApp.bulkUploads().newFileUpload(sheet_add);
  upload.forCampaignManagement();
  // Use upload.apply() to make changes without previewing.
  // upload.preview();
  upload.apply();
}サンプルコードの説明
サンプルコードでは、以下6つの処理を行っています。
STEP2とSTEP4は同じ処理を行っています。
スプレッドシートのシート情報の定義
// スプレッドシートを指定
var spreadsheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/スプレッドシートID/edit#gid=0');
// シートを指定
var sheet_negativeKeywords = spreadsheet.getSheetByName('negativeKeywords');
// シートを指定
var sheet_add = spreadsheet.getSheetByName('add');除外キーワードリストに登録されているキーワードを最新に更新
function getNegativeKeywords () {
  
  // 除外キーワードリストのキーワード一覧を取得
  var report = AdsApp.report(
      "SELECT shared_set.name, shared_criterion.keyword.text, shared_criterion.keyword.match_type FROM shared_criterion WHERE shared_criterion.type = 'KEYWORD'"
  );
  // レポートの結果をスプレッドシートに書き込む
  report.exportToSheet(sheet_negativeKeywords);
}除外キーワードを除外キーワードリストに入稿
function addNegativeKeywords () {
  // The format of this spreadsheet should match a valid bulk upload template.
  // See https://developers.google.com/google-ads/scripts/docs/features/bulk-upload
  // for the list of supported bulk upload templates.
  // bulkupload
  const upload = AdsApp.bulkUploads().newFileUpload(sheet_add);
  upload.forCampaignManagement();
  // Use upload.apply() to make changes without previewing.
  // upload.preview();
  upload.apply();
}スプレッドシートのaddシートで抽出された除外キーワードを入稿します。
さいごに
今回はGoogle広告スクリプトを用いた除外キーワードリストへの入稿方法を解説しました。
本スクリプトを活用することで除外キーワードの設定作業の効率化にお役立ていただければ嬉しく思います。

