Javascript 对象正在覆盖自身

时间:2021-05-13 14:32:18

标签: javascript google-apps-script

我正在尝试将从 Google 表格中提取的一些数据解析为 javascript 对象,以在 google 应用程序脚本中创建一些附加链接扩展。我解析数据的代码是:

function parseData(sheetData)
{
  var parsedData = {};
  var header = sheetData.shift()
  for(lineIndex in sheetData)
  {
    var accountID = sheetData[lineIndex][0]
    var campaignName = sheetData[lineIndex][1]
    var description1 = sheetData[lineIndex][2]
    var description2 = sheetData[lineIndex][3]
    var url = sheetData[lineIndex][4]
    
    if(!(accountID in Object.keys(parsedData)))
    {
      parsedData[accountID] = {}
    }
    if(!(campaignName in Object.keys(parsedData[accountID])))
    {
      var tempObject = {}
      parsedData[accountID][campaignName] = tempObject
      for(columnIndex in sheetData[lineIndex])
      {
        //parsedData[accountID][campaignName][header[columnIndex]] = sheetData[lineIndex][columnIndex]
        tempObject[header[columnIndex]] = sheetData[lineIndex][columnIndex]
      }
      parsedData[accountID][campaignName] = tempObject
    }
  }
  return parsedData
}

我这样做是为了在接下来的步骤中通过 accountID 和 campaignName 检索信息。

但每次创建“campaignName”对象时,它最终都会覆盖自己,并且只保存最后一个campaignName对象,每个accountID对象只保存一个。

有人知道为什么会这样吗?

编辑: 我期待得到以下形状的对象:

{{111-111-2222=
    {campaignName1=
        {description1=best prices available, 
        url=https://exampleurl.com},
    {campaignName2=
        {description1=Em até 12x sem Juros, 
        url=https://exampleurl.com}
},
{111-111-3333=
    {campaignName1=
        {description1=best prices available, 
        url=https://exampleurl.com},
    {campaignName2=
        {description1=Em até 12x sem Juros, 
        url=https://exampleurl.com}
}}

所以,当我执行我的代码时,我可以通过以下方式访问数据:

var currentAccountID = AdsApp.currentAccount().getCustomerId();
  var campaignNames = Object.keys(sitelinkData[currentAccountID])
  
  var campaigns = AdsApp.campaigns()
                        .withCondition("CampaignStatus='ENABLED'")
                        .get()
  while(campaigns.hasNext())
  {
    var campaign = campaigns.next()
    var campaignName = campaign.getName()
    var description1 = parsedData[currentAccountID][campaignName]["Description1"]
}
}

2 个答案:

答案 0 :(得分:2)

这里有一些可能有用的东西。很难测试,因为没有数据。但是我认为您遇到的问题是您正在分配 parsedData[accountID][campaignName] = tempObject 而不是将 tempObject 附加到现有的 parsedData[accountID][campaignName]

function parseData(sheetData)
{
  var parsedData = {};
  var header = sheetData.shift()
  for(lineIndex in sheetData)
  {
    var accountID = sheetData[lineIndex][0]
    var campaignName = sheetData[lineIndex][1]
    var description1 = sheetData[lineIndex][2]
    var description2 = sheetData[lineIndex][3]
    var url = sheetData[lineIndex][4]
    
    
    if(!parsedData[accountId])
    {
      parsedData[accountID] = []
    }
    if(!parsedData[accountID][campaignName])
    {
      parsedData[accountID][campaignName] = {} // <= instantiate here
    }
    for(columnIndex in sheetData[lineIndex])
    {
      parsedData[accountID][campaignName][header[columnIndex]] = sheetData[lineIndex][columnIndex]
    }
  }
  return parsedData
}

答案 1 :(得分:0)

我添加了一些数据,按原样运行脚本,并获得一些输出。现在你能告诉我们你想在这个输出中改变什么吗?

var data = [
    ["accountID", "campaignName", "description1", "description2", "url"],
    ["id1", "company name 1", "info 1", "info 1", "url1"],
    ["id2", "company name 2", "info 2", "info 2", "url2"],
    ["id3", "company name 3", "info 3", "info 3", "url3"]
];


function parseData(sheetData)
{
  var parsedData = {};
  var header = sheetData.shift()
  for(lineIndex in sheetData)
  {
    var accountID = sheetData[lineIndex][0]
    var campaignName = sheetData[lineIndex][1]
    var description1 = sheetData[lineIndex][2]
    var description2 = sheetData[lineIndex][3]
    var url = sheetData[lineIndex][4]

    if(!(accountID in Object.keys(parsedData)))
    {
      parsedData[accountID] = {}
    }
    if(!(campaignName in Object.keys(parsedData[accountID])))
    {
      var tempObject = {}
      parsedData[accountID][campaignName] = tempObject
      for(columnIndex in sheetData[lineIndex])
      {
        //parsedData[accountID][campaignName][header[columnIndex]] = sheetData[lineIndex][columnIndex]
        tempObject[header[columnIndex]] = sheetData[lineIndex][columnIndex]
      }
      parsedData[accountID][campaignName] = tempObject
    }
  }
  return parsedData
}

console.log(parseData(data));

输出:

{
  id1: {
    'company name 1': {
      accountID: 'id1',
      campaignName: 'company name 1',
      description1: 'info 1',
      description2: 'info 1',
      url: 'url1'
    }
  },
  id2: {
    'company name 2': {
      accountID: 'id2',
      campaignName: 'company name 2',
      description1: 'info 2',
      description2: 'info 2',
      url: 'url2'
    }
  },
  id3: {
    'company name 3': {
      accountID: 'id3',
      campaignName: 'company name 3',
      description1: 'info 3',
      description2: 'info 3',
      url: 'url3'
    }
  }
}

我从接受的答案中截取了片段(修正了一个错字)并在同一日期运行它。这是输出:

{
  id1: [
    'company name 1': {
      accountID: 'id1',
      campaignName: 'company name 1',
      description1: 'info 1',
      description2: 'info 1',
      url: 'url1'
    }
  ],
  id2: [
    'company name 2': {
      accountID: 'id2',
      campaignName: 'company name 2',
      description1: 'info 2',
      description2: 'info 2',
      url: 'url2'
    }
  ],
  id3: [
    'company name 3': {
      accountID: 'id3',
      campaignName: 'company name 3',
      description1: 'info 3',
      description2: 'info 3',
      url: 'url3'
    }
  ]
}

除了 {} 变成 [](由于另一个错误或有意更改已接受的答案)外,我没有看到任何区别。

那么,哪里出了问题?


据我所知,OP 应该是这样的:

var data = [
  ["accountID", "CampaignName", "Description1", "Description2", "url"],
  ["id1", "company 1", "description company 1", "description2 company 1", "url 1"],
  ["id2", "company 2", "description company 2", "description2 company 2", "url 2"],
  ["id3", "company 3", "description company 3", "description2 company 3", "url 3"]
];

function parseData(sheetData) {

  var header = sheetData.shift();
  var companies = {}

  for (var row in sheetData) {
    var campaignName = sheetData[row][1];
    companies[campaignName] = {};
    companies[campaignName][header[2]] = sheetData[row][2]; // description1
    companies[campaignName][header[4]] = sheetData[row][4]; // url
    // etc for any column in your table
  }

  var parsedData = {}
  sheetData.forEach(r => parsedData[r[0]] = companies);
  return parsedData;
}

var parsedData = parseData(data);
var id_company_description = parsedData["id1"]["company 2"]["Description1"];

console.log(parsedData); // see output
console.log(id_company_description); // "description company 2"

输出:

{
  id1: {
    'company 1': { Description1: 'description company 1', url: 'url 1' },
    'company 2': { Description1: 'description company 2', url: 'url 2' },
    'company 3': { Description1: 'description company 3', url: 'url 3' }
  },
  id2: {
    'company 1': { Description1: 'description company 1', url: 'url 1' },
    'company 2': { Description1: 'description company 2', url: 'url 2' },
    'company 3': { Description1: 'description company 3', url: 'url 3' }
  },
  id3: {
    'company 1': { Description1: 'description company 1', url: 'url 1' },
    'company 2': { Description1: 'description company 2', url: 'url 2' },
    'company 3': { Description1: 'description company 3', url: 'url 3' }
  }
}

description company 2