简体   繁体   English

从 Google 表格导入数据

[英]Import data from Google sheets

I retrieve keyword IDs and clicks from a Google ads account and type them into Google sheets.我从 Google 广告帐户中检索关键字 ID 和点击次数,并将它们输入到 Google 表格中。 In Google sheets, I do some calculations before I want to pull keyword ID back to Google ads.在 Google 表格中,我会先进行一些计算,然后再将关键字 ID 拉回 Google 广告。

How do I do it?我该怎么做?

function main() {

  //Step 1: Connect Google Ads to the Google Sheet
  var spreadsheetUrl = 'https://docs.google.com/spreadsheets/;
  var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
  var ss = spreadsheet.getSheetByName('data');
  ss.clear();

 //Step 2: Create an array to store the data
  var sheetarray = [['clicks', 'ID']];

  //Step 3: Collect the data you need
  var keywords = AdsApp.keywords()
      .withCondition("Clicks > 0")
      .forDateRange("LAST_7_DAYS")
      .get();
  
  //Step 4: Add the data you got from Google Ads into the array
while (keywords.hasNext()) {
  var keyword = keywords.next();
  sheetarray.push([
      keyword.getStatsFor("LAST_7_DAYS").getClicks(),
      keyword.getId(),
    ]);
}

   
  //Step 5: Display the contents of the array
  Logger.log(sheetarray);
  if (sheetarray.length > 0) {
     
    // Step 6: Send the array's data to the Google Sheet
    ss.getRange(1, 1, sheetarray.length, sheetarray[0].length).setValues(sheetarray);
  }
  

  //Step 6: Get keyword IDs from the sheets but how??
  
  
   
}


var keyword_IDs = ss.getRange(2, 2, sheetarray.length-1, 1).getValues().flat();

Probably大概


flat() does not work, but join() does flat() 不起作用,但 join() 可以

I don't know what you're trying to get.我不知道你想得到什么。 But it looks a bit wrong.但它看起来有点不对劲。 flat() and join() have very different output. flat()join()有非常不同的 output。 flat() returns Array . flat()返回Array join() returns String join()返回String

var keyword_IDs = ss.getRange(2, 2, sheetarray_length-1, 1).getValues().flat();

Logger.log(keyword_IDs)                            // [id1, id2, id3, id4, id5]
Logger.log(keyword_IDs.constructor.name);          // Array
Logger.log(keyword_IDs.filter(x => x==",").length) // 0.0 -- it has no comas

var keyword_IDs = ss.getRange(2, 2, sheetarray_length-1, 1).getValues().join();

Logger.log(keyword_IDs);                           // "id1,id2,id3,id4,id5"
Logger.log(keyword_IDs.constructor.name);          // String
Logger.log(keyword_IDs.match(/,/g).length)         // 4.0 -- it has four comas

I get the error: TypeError: Cannot find function map in object ''' var CampaignExact = AdsApp.campaigns().withCondition(Campaign_Exact).get().next();我收到错误:TypeError: Cannot find function map in object ''' var CampaignExact = AdsApp.campaigns.get().with.Condition() var keywords = CampaignExact.keywords().withIds(keyword_ID).get(); var 关键字 = CampaignExact.keywords().withIds(keyword_ID).get(); while(keywords.hasNext()) { var keyword = keywords.next(); while(keywords.hasNext()) { var 关键字=keywords.next(); keyword.pause();关键字.pause(); } ''' } '''

It's almost indecipherable message.这是几乎无法辨认的信息。 Sorry.对不起。

The error says about map() function.错误说关于map() function。 But I see no map() in your code.但是我在您的代码中没有看到map() Nevertheless map() is a Array's method.然而map()是一个数组的方法。 Not String's.不是字符串的。

So I guess withIds(keyword_ID) don't work since keyword_ID is a String like this 'id1,id2,id3...' .所以我猜withIds(keyword_ID)不起作用,因为keyword_ID是这样的字符串'id1,id2,id3...' Probably there should be an array keywords_ID ( ['id1', 'id2', 'id3', ...] ) and then there should be keyword_ID = keywords_ID[0] or keyword_ID = keywords_ID[1] ...etc somewhere above.可能应该有一个数组keywords_ID['id1', 'id2', 'id3', ...] )然后应该有keyword_ID = keywords_ID[0]keyword_ID = keywords_ID[1] ...等等上面的某个地方. Just a guess.只是一个猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM