合并重复的行并设置值

时间:2018-09-05 09:20:16

标签: google-apps-script google-sheets google-adwords

我对使用Google表格编写脚本非常陌生,但是我在这个论坛上进行了搜索,却找不到答案。

通过google广告脚本开发人员的脚本,我设法创建了所有激活我的google广告的搜索字词的导出。这些关键字正在与我帐户中当前使用的关键字进行比较。这样,我可以找到帐户中缺少的关键字以及效果不佳的关键字。

此脚本将关键字输出到Google表格文档中,但是,如果多个露营和/或广告组中存在关键字,则会为每个广告系列/广告组分别创建一个关键字行,并为其指定相应的值。

但是,我真的很想将这些值合并到同一行中,以便每个关键字只有一行,而它们的唯一值仅合并为一个值。 This is the current output of script in google sheets

and this is how i would like to have the output

我真的不知道如何实现这一目标,有人可以帮我解决这个问题吗?不胜感激!

我正在运行以获取导出的代码添加在下面。

// Add your spreadsheet here
var SPREADSHEET_URL = 'Google sheets url';

// This is quereis minmum number of impressions.
var Impressions_Threshold = 50;

// If the CTR is below the threshold, it will be labeled as "Not excluded and low performing"
var CTR_Threshold = 1;

// If the number of conversions is above the threshold and the query is not added, it will be labeled as "Not added and has conversions"
var Conversions_Threshold = 0;

// Add your email here
var Notify_Me = "email"

function main() {

    var sheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getActiveSheet();
    var columns = [
        'CustomerDescriptiveName',
        'ExternalCustomerId',
        'Query',
        'CampaignName',
        'AdGroupName',
        'Impressions',
        'Clicks',
        'Cost',
        'Ctr',
        'Conversions'
    ];

    sheet.clearContents();
    sheet.appendRow(columns);
    sheet.getRange("K1").setValue("Added Or Not");

    // Get all the keywords in all the acccounts
    var allKeywords = [];
    var report = AdWordsApp.report(
        "SELECT AdGroupId, Criteria " +
        "FROM KEYWORDS_PERFORMANCE_REPORT " +
        "WHERE KeywordMatchType = EXACT " +
        "DURING TODAY");
    var rows = report.rows();
    while (rows.hasNext()) {
        var row = rows.next();
        var KeywordsLower = row['Criteria'].toLowerCase();
        allKeywords.push(KeywordsLower);
    }

    // Get all the search queries in all the accounts
    var positiveKeywords = [];
    var columnsStr = columns.join(',') + " ";

    var SQRreport = AdWordsApp.report(
        'SELECT ' + columnsStr +
        ' FROM SEARCH_QUERY_PERFORMANCE_REPORT ' +
        ' WHERE ' +
        ' Impressions > ' + Impressions_Threshold +
        ' DURING 20120215,20180904'
    );

    var rows = SQRreport.rows();
    while (rows.hasNext()) {
        var row = rows.next();
        positiveKeywords.push(row['Query']);

        for (var i = 0; i < positiveKeywords.length; i++) {
            if ((allKeywords.indexOf(positiveKeywords[i]) === -1) && (row['Conversions'] > Conversions_Threshold)) {
                row.addedOrNot = 'Not Added and has Conversions';
            } else if ((allKeywords.indexOf(positiveKeywords[i]) === -1) && (parseFloat(row['Ctr']) < CTR_Threshold)) {
                row.addedOrNot = 'Not excluded and low performing';

            } else if (allKeywords.indexOf(positiveKeywords[i]) === -1) {
                row.addedOrNot = 'Not Added';
            } else {
                row.addedOrNot = 'Added'
            }
        }
        sheet.appendRow([row['CustomerDescriptiveName'], row['ExternalCustomerId'], row['Query'], row['CampaignName'], row['AdGroupName'], row['Impressions'], row['Clicks'], row['Cost'], row['Ctr'], row['Conversions'], row['addedOrNot']]);
    }

    MailApp.sendEmail(Notify_Me,
        'SQR Report',
        'Your SQR report for this month is ready, please visit ' + SPREADSHEET_URL + ' Thanks');

}

1 个答案:

答案 0 :(得分:1)

您可以 编写脚本,但是从长远来看,最好保留出口原样,而让第二张表按您的意愿显示数据。我说这是细粒度的行数据,对于不同的分析而言可能更可取。

在第二张工作表中,您可以使用Google Spreadsheet公式Query(),并通过关键字获得该组结果。如果您需要在演示文稿表中使用所有相同的列,那么您会知道的最好。

如果您知道任何类似SQL的数据库,则查询结构应该很熟悉。如果不是这样,那将是非常简单的,但是您可以在此处了解更多信息: