【问题标题】:Remove the line title from the JSON structure从 JSON 结构中删除行标题
【发布时间】:2017-11-08 23:18:56
【问题描述】:

这是我的电子表格的link

//max CPC
var maxCPC = 3.00;
//min CPC
var minCPC = 0.50;
//percentage change down


function main() {
    var SPREADSHEET_URL = "https://docs.google.com/spreadsheets/d/1QGJU5FJVmKI3A5A3NFY-XQU7MlBOG0VmvUIZvx8Bitg/edit#gid=72338765";

    var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
    var sheet = spreadsheet.getSheetByName('Campaigns');
    var data = sheet.getRange("A:C").getValues();

    var test = parseData(data)
}


function isBlank(line) {
    return line[0].trim() === '' && line[1].trim() === '';
}


function parseData(data) {

    const output = {};
    var currentGroupName = '';

    data.forEach(function(line){
        if (isBlank(line)){
            return; 
        }

        if (line[0].trim().length > 1) {
            currentGroupName = line[0].trim();
        }

        output[currentGroupName] = output[currentGroupName] || {};

        output[currentGroupName][line[1]] = line[2];
    });

    return output;
}

此代码允许我将 Google 电子表格重新格式化为 JSON 结构。我不想包括第一行,因为它是标题行,但我不知道该怎么做。如何修改我的代码以使其正常工作?

【问题讨论】:

  • 在检索数据时忽略标题行怎么样?那么从var data = sheet.getRange("A:C").getValues();修改为var data = sheet.getRange("A2:C").getValues();怎么样?
  • 跳过第一行可以使用data.slice(1)

标签: javascript google-apps-script spreadsheet


【解决方案1】:

如果您不关心内存使用情况,这是使用 ES2015 destructuring assignment 的理想选择,它可以让您将 data 数组分解为一个新的 header 数组和一个包含你想要的行数组。以下是您的代码使用它的样子:

function parseData(data) {

    const output = {};
    var currentGroupName = '';
    [header, ...lines] = data;  // break data into the first item, 'header', and the rest of the items, 'lines'

    lines.forEach(function(line){
        if (isBlank(line)){
            return; 
        }

        if (line[0].trim().length > 1) {
            currentGroupName = line[0].trim();
        }

        output[currentGroupName] = output[currentGroupName] || {};

        output[currentGroupName][line[1]] = line[2];
    });

    return output;
}

但是,如果您不想复制数据数组,也可以在 forEach 调用中包含 index 参数,并使用它来跳过第一行数据当您遍历这些行时:

function parseData(data) {

    const output = {};
    var currentGroupName = '';

    data.forEach(function(line, index){
        if (isBlank(line) || index === 0){
            return; 
        }

        if (line[0].trim().length > 1) {
            currentGroupName = line[0].trim();
        }

        output[currentGroupName] = output[currentGroupName] || {};

        output[currentGroupName][line[1]] = line[2];
    });

    return output;
}

【讨论】:

    【解决方案2】:

    大多数解析器包含一个问题是有原因的:“您的文件是否包含标题?” (或其一些变体)但是,如果您始终保证有一个标题行,您可以更改您的解析例程,如下所示:

    function parseData(data) {
    
        const output = {};
        var currentGroupName = '';
        var skipLine = 1;
    
        data.forEach(function(line){
            if (isBlank(line)){
                return; 
            }
            if (skipLine == 1) {
                skipline = 0;
            }
            else {
                if (line[0].trim().length > 1) {
                    currentGroupName = line[0].trim();
                }
    
                output[currentGroupName] = output[currentGroupName] || {};
    
                output[currentGroupName][line[1]] = line[2];
            } 
        });
    
        return output;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 1970-01-01
      • 2013-04-25
      • 2022-06-10
      • 2019-01-15
      • 2016-10-27
      • 1970-01-01
      • 2020-08-14
      • 2017-06-03
      相关资源
      最近更新 更多