Google apps script Google脚本-解析json响应

Google apps script Google脚本-解析json响应,google-apps-script,google-sheets,jsonparser,Google Apps Script,Google Sheets,Jsonparser,这是我试图解析的多选字段的json响应: {所选:true,禁用:false,text:Ctr,id:Ctr,title:,_resultId:select2-selectcountry-result-je97-Ctr,元素:{} {选定:真,禁用:假,文本:标题 第1部分,id:TitlePart1,title:,_resultId:select2-selectcountry-result-uv7s-TitlePart1,元素:{} {已选择:false,已禁用:false,文本:Milan,

这是我试图解析的多选字段的json响应:

{所选:true,禁用:false,text:Ctr,id:Ctr,title:,_resultId:select2-selectcountry-result-je97-Ctr,元素:{}

{选定:真,禁用:假,文本:标题 第1部分,id:TitlePart1,title:,_resultId:select2-selectcountry-result-uv7s-TitlePart1,元素:{}

{已选择:false,已禁用:false,文本:Milan,id:Milan,_resultId:select2 selectcountry结果bmba Milan,元素:{}]

我需要得到:{id:value},{id:value},{id:value}

{id:Ctr},{"id":"TitlePart1"},{"id":"Milan} ...
为了实现这个结果,我使用以下代码:

var response = (JSON.stringify($('#selectcountry').select2('data')) );        
var json = JSON.parse(response);
        var dataSet = json;
        var row = [],
        data;
       for(var i in json){
       data = dataSet[i];
       row.push({'id': json[i].id})
       }

   sheet.getRange(6,1).setValue(row);
但通过这种方式,我只得到第一个id:value:

{id:Ctr}
有什么帮助吗


谢谢

使用设置值instead@TheMaster我试过了。设置值如下图所示;当使用.setValues[row]时,没有得到任何结果;我只选择了第一个值。您似乎缺少带括号的开头方块。这是故意的吗?谢谢你的帮助Cooper。使用您的代码,日志向我显示3个ID[Ctr,TitlePart1,Milan],但当我使用公式sheet.getRange6,1.setValueID在工作表中设置值时;我只得到了第一个Ctr值。这不是一个自定义单元格公式吗?我怀疑cell公式是否有权访问JSON.parse,并且不能使用cell公式更改单元格周围的内容。您需要阅读documenation.No中的单元格公式限制。对不起我的英语。使用sheet.getRange6,1.setValueId;在记录器下面。当我在工作表上运行脚本时,即使日志中有[Ctr,TitlePart1,Milan],也只显示值Ctr。首先,ID是一个数组,而不是一个值。其次,您不能真正使用setValues,因为ids不是2d数组,但您可以使用setValues[ids]将其作为行发布
var s='[{"selected":true,"disabled":false,"text":"Ctr","id":"Ctr","title":"","_resultId":"select2-selectcountry-result-je97-Ctr","element":{}},{"selected":true,"disabled":false,"text":"Title Part1","id":"TitlePart1","title":"","_resultId":"select2-selectcountry-result-uv7s-TitlePart1","element":{}},{"selected":false,"disabled":false,"text":"Milan","id":"Milan","_resultId":"select2-selectcountry-result-bmba-Milan","element":{}}]';

function findId() {
  var d=JSON.parse(s);
  var ids=[];
  d.forEach(function(o){
    ids.push(o.id);
  });
  Logger.log(ids);
  //Add this
  SpreadsheetApp.getActiveSheet().getRange(1,1,1,3).setValues([ids]);
}