How to appendRow in specific spreadsheet column

4,145 views
Skip to first unread message

rajinderd...@unito.at

unread,
Mar 28, 2019, 12:01:38 PM3/28/19
to Google Ads Scripts Forum
I want to appendrow weekly in a specific spreadsheet column.
With this code it appendrow in the first column.
I want it in the 3rd Column. 
I have tried it with sheet.getRange (1,3).setValue(numKeywords)  but that it overwrite the 
cell. I need a weekly history. 


CODE:
function main() {  
  
  function main() {  
  
  var targetSheet = 'QDE';
  var sheetID = '1pbF7flPcD5IsDyiC_KMSNK5PCL8qwBedsHjaooEo1go';
  var spreadsheet = SpreadsheetApp.openById(sheetID);
  
  
  var sheet = spreadsheet.getSheetByName(targetSheet);



  var keywords = AdsApp.keywords()
      .forDateRange("TODAY")
      .withCondition("Status = ENABLED")
  .withCondition("CampaignStatus = ENABLED")
 .withCondition("AdGroupStatus = ENABLED")
      .get();
  var numKeywords = keywords.totalNumEntities();
  
 sheet.appendRow([numKeywords]);
    //Logger.log("Total keywords: " + account.getName() + " " + numKeywords);
 
 
  
}

googleadsscrip...@google.com

unread,
Mar 28, 2019, 3:12:34 PM3/28/19
to adwords-scripts+apn2wqf58hlmcjgo...@googlegroups.com, Google Ads Scripts Forum
Hello,

One way to do this would be to create a function that takes the number of total keywords, the row, and the column, as parameters. Then the function would write the number to that specific row and column. For example,

function setValueInSpreadsheet(val, row, col) {
  
// Insert spreadsheet URL
  
var SPREADSHEET_URL = 'INSERT_SPREADSHEET_URL';
  
// Name of the specific sheet in the spreadsheet.
  
var SHEET_NAME = 'SheetName';

  
var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
  
var sheet = ss.getSheetByName(SHEET_NAME);

  
var range = sheet.getRange(row,col);
  
var value = range.getValue();
  
  range
.setValue(val);
  
}

An example of main() is below. Note the function call in bold:

function main() {
  
  
// Second row, third column, for example
  
var row = 2;
  
var col = 3;

  
  
var keywords = AdsApp.keywords()
  
.forDateRange("TODAY")
  
.withCondition("Status = ENABLED")
  
.withCondition("CampaignStatus = ENABLED")
  
.withCondition("AdGroupStatus = ENABLED")
  
.get();
  
  
var numKeywords = keywords.totalNumEntities();

  setValueInSpreadsheet(numKeywords, row, col);
  
}


Regards,
Matt
Google Ads Scripts Team

--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scripts+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/274d8d6e-1277-4d7b-a9e9-d64406c87a01%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages