Transfer results from Execution Log to Google Sheet

I am stocked with my code here that I got from Google Analytics, my goal is to have the results from my execution log to my Google sheet file. I am already good with what I have in the log and just display the results as google sheet data.

Here's my code:

function listAccounts() {
  var accounts = Analytics.Management.Accounts.list();

  if (accounts.items && accounts.items.length) {
      var sheet = SpreadsheetApp.getActiveSpreadsheet();
      var dataSheet = sheet.getSheetByName('DATA');
      
      for (var i = 0; i < accounts.items.length; i++) {
      var account = accounts.items[i];

      Logger.log('Account Name: ' + account.name);
      Logger.log('Account Id: ' + account.id);
      //Logger.log('Account Name "%s", id "%s".', account.name, account.id);
    }

  } else {
    Logger.log('No accounts found.');
  }
}

Thank you for the help.


If you just want to write into a sheet, what you get in your Logger.log(), then you can do it like this:

function listAccounts() {
  //your log sheet, use getActive() if it's the same sheet
  var logSheet = SpreadsheetApp.openById('sheetid').getSheetByName('Sheet name');
  var accounts = Analytics.Management.Accounts.list();

  if (accounts.items && accounts.items.length) {
      var sheet = SpreadsheetApp.getActiveSpreadsheet();
      var dataSheet = sheet.getSheetByName('DATA');
      
      for (var i = 0; i < accounts.items.length; i++) {
      var account = accounts.items[i];

      Logger.log('Account Name: ' + account.name);
      Logger.log('Account Id: ' + account.id);
      logSheet.appendRow(['Account Name: ' + account.name, 'Account Id: ' + account.id]);
      //Logger.log('Account Name "%s", id "%s".', account.name, account.id);
    }

  } else {
    Logger.log('No accounts found.');
  }
}