Zahir
Zahir

Reputation: 13

Pushing a simple Log string from a Google Ad Script to a Google Sheet

I am trying to set up a script which can push data from an App Script into a Google Sheet.

I have the script successfully logging what I want, which goes in the following format Account budget is 12344, but now I want to push this into a Google Sheet. I have set up a variable containing the URL and another variable containing the sheet name, and also a clear method to delete anything already there.

Find the code I have below:

// - The link to the URL
var SPREADSHEET_URL = 'abcdefghijkl'
// - The name of the sheet to write the data
var SHEET_NAME = 'Google';


// No to be changed 
function main() { 
  var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
  var sheet = spreadsheet.getSheetByName(SHEET_NAME);
  sheet.clearContents();  
}


function getActiveBudgetOrder() {
  // There will only be one active budget order at any given time.
  var budgetOrderIterator = AdsApp.budgetOrders()
      .withCondition('status="ACTIVE"')
      .get();
  while (budgetOrderIterator.hasNext()) {
    var budgetOrder = budgetOrderIterator.next();
    Logger.log("Budget Order Amount " + budgetOrder.getSpendingLimit());
  }
}

Upvotes: 0

Views: 119

Answers (1)

gries
gries

Reputation: 260

Assuming you want to clear the entire Sheet every time you extract the data this should work for you. You will need to set the url and shtName variables.

function getActiveBudgetOrder() {

  var url = 'https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxxx/';
  var shtName = 'Sheet1';
  var arr = [];

  var sht = SpreadsheetApp.openByUrl(url).getSheetByName(shtName);

  // There will only be one active budget order at any given time.
  var budgetOrderIterator = AdsApp.budgetOrders()
      .withCondition('status="ACTIVE"')
      .get();
  while (budgetOrderIterator.hasNext()) {
    var budgetOrder = budgetOrderIterator.next();
    arr.push(["Budget Order Amount " + budgetOrder.getSpendingLimit()]);
  }

  sht.clearContents();
  sht.getRange(1, 1, arr.length, arr[0].length).setValues(arr);

}

Upvotes: 1

Related Questions