如果您有多個帳戶或代替其他人管理帳戶,一切都會從 AccountsApp 物件開始。 AccountsApp 是最上層物件,可用來取得您有權存取的帳戶清單,以及選取要管理的帳戶。 取得並選取帳戶之後,您可以切換至使用 AdsApp 物件來存取帳戶的實體。
注意事項
若為多帳戶指令碼,請使用可從 Microsoft Advertising Web 應用程式中的 [帳戶摘要 ] 存取的指令碼編輯器。 如果您在UI中看不到 帳戶摘要 ,表示您不會使用多帳戶指令碼編輯器。
若要從 [帳戶摘要] 存取指令碼編輯器,請按一下左窗格中的 [ 大量作業 ]。 接著,在 [指令碼] 下,按一下 [建立和管理指令碼]。
列出您可以存取的帳戶
若要列出您有存取權的所有帳戶,請呼叫 accounts 方法。 這個方法會傳回 BingAdsAccountSelector 物件,可用來篩選帳戶清單。 如需使用選取器來篩選清單的詳細資訊,請參閱 使用選取器。
下列範例會傳回您有存取權的所有帳戶。
function main() {
var accounts = AccountsApp.accounts()
.get();
while (accounts.hasNext()) {
var account = accounts.next();
Logger.log(`Account ID: ${account.getAccountId()}
Account name: ${account.getName()}
Account number: ${account.getAccountNumber()}
Customer ID: ${account.getCustomerId()}
Currency code: ${account.getCurrencyCode()}
Time zone: ${account.getTimeZone()}\n\n`);
}
}
同時執行每個帳戶的函數
若要同時在多個帳戶上執行工作,請呼叫 選取器的方法executeInParallel() 。 以下是您可以呼叫的方法 executeInParallel() 。
executeInParallel (字串 functionName, 字串 optionalCallbackFunctionName)
指定指令碼針對選取器傳回的每個帳戶呼叫的函式名稱。 函數可能會以字串的形式傳回值。 若要傳回複雜的物件,請使用 JSON.stringify 方法將物件轉換成字串。 然後,您可以使用 JSON.parse 方法將字串轉換回物件。
如果您的函式傳回值,則必須指定回呼函式來擷取傳回值。 為所有選取的帳戶執行函式之後,指令碼會呼叫選用的回呼函式。 傳回值會以 ExecutionResult 物件的陣列形式傳遞。
executeInParallel (字串 functionName, 字串 optionalCallbackFunctionName, 字串 optionalInput)
指定指令碼針對選取器傳回的每個帳戶呼叫的函式名稱。 您可以指定指令碼傳遞給函數的選用輸入字串。 若要傳遞複雜的物件,請使用 JSON.stringify 方法將物件轉換成字串。 接著,您可以在函數內使用 JSON.parse 方法,將字串轉換回物件。
函數可能會以字串的形式傳回值。 若要傳回複雜的物件,請使用 JSON.stringify 方法將物件轉換成字串。 然後,您可以使用 JSON.parse 方法將字串轉換回物件。
如果您的函式傳回值,則必須指定回呼函式來擷取傳回值。 為所有選取的帳戶執行函式之後,指令碼會呼叫選用的回呼函式。 傳回值會以 ExecutionResult 物件的陣列形式傳遞。
您必須將帳戶數目限制為 50 個,否則,如果選取器傳回超過 50 個,則呼叫會失敗。 若要限制帳戶數目,您可以使用 withLimit()、 withIds()及 withAccountNumbers() 方法。
下列範例顯示一個簡單的範例,該範例會針對上週點選率低於 5% 的每個帳戶執行函式。 此範例會使用此 withLimit() 方法來確保呼叫不會超過 50 個帳戶的限制。
function main() {
// Select the accounts to process.
var accounts = AccountsApp.accounts()
.withLimit(50)
.withCondition('Ctr < 0.05')
.forDateRange('LAST_WEEK')
.executeInParallel('bump', 'resultsHandler');
}
function bump() {
var account = AdsApp.currentAccount();
// Do something with the entities in the account.
Logger.log(`Processing account: ${account.getAccountId()} (${account.getName()})`);
// Return a value that's processed by resultsHandler(). If
// the function returns a value, it must be a string. To return
// a complex object, use JSON.stringify(object) to return the
// object as a string.
return account.getAccountId();
}
// Handles all return values from the bump() function after the
// function completes for all accounts.
function resultsHandler(results) {
for (var result of results) {
if (result.getStatus() === 'OK') {
value = result.getReturnValue();
}
}
}
變更指令碼處理的帳戶
在您選取要處理的帳戶之前,您無法呼叫任何 AdsApp 方法來取得帳戶的實體資料。 若要選取帳戶,請使用 AccountsApp 的 select 方法。
但首先您需要呼叫 帳戶 方法來選取您要處理的帳戶。 如需用 accounts() 來篩選帳戶清單的相關資訊,請參閱 列出您有存取權的帳戶。
取得帳戶後,呼叫 select() 方法,將帳戶設為活期帳戶。 下列範例顯示此流程。
function main() {
// This call logs null. Before using any
// of the AdsApp methods, you must first
// select an account to process.
Logger.log(AdsApp.currentAccount());
// Select the accounts to process
var accounts = AccountsApp.accounts()
.withIds(['123', '456', '789'])
.get();
while (accounts.hasNext()) {
AccountsApp.select(accounts.next());
// AdsApp is now set to the current account.
// Do something with the account's entities.
}
}