【问题标题】:Parse strings and objects from value of cell in spreadsheet从电子表格中的单元格值解析字符串和对象
【发布时间】:2018-02-21 02:05:45
【问题描述】:

我得到了包含令人讨厌的值的电子表格,并且为此而苦苦挣扎。所需数据在 A 列的每个单元格中。单元格的值为{"p0":70,"u3":71,"s7110":40},t45,{"t78":60,"s3310":15},p37,p36,{"p29":44,"s8110":95},p85,p14,{"s2710":41},u47。此类值的数量约为 1000。我必须解析这些值。我需要来自这些值的字符串和对象。我可以忽略解析值的顺序。我无法手动解析。所以我决定使用脚本。

我尝试使用“split”进行解析。

var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getRange("A1:A" + sheet.getLastRow()).getValues();
var result = [];
for (var i = 0; i < values.length; i++)
{
  result.push(values[i][0].split(","));
}

我得到t45,p37,p36,p85,p14,u47 作为字符串。但是“拆分”也会拆分所有对象。例如,{"p0":70,"u3":71,"s7110":40} 拆分为 {"p0":70"u3":71"s7110":40}。有办法解决吗?

示例值是这个。每一行都在单元格 A1、A2、A3、A4、A5 中。

{"p0":70,"u3":71,"s7110":40},t45,{"t78":60,"s3310":15},p37,p36,{"p29":44,"s8110":95},p85,p14,{"s2710":41},u47
s6610,{"t25":70,"u8":43,"p35":86},u85,u74,{"s7710":83},{"p70":70,"u67":84},{"u71":43,"s1210":73},{"u45":84,"s710":15},{"u14":79,"p22":45},p31
u73,u12,{"t51":98,"u57":96},u31,p41,s1110,s6610,p55,{"t57":71,"s7510":83,"u62":17},u73
t50,{"t83":22,"p18":76},{"p47":12,"s8710":18,"u11":35},{"t14":74,"u72":51},{"p74":21,"t77":77},{"u62":84,"s3010":11},p81,u36,p67,{"t79":12,"u2":70,"s6010":98}
{"u54":51,"t31":31},t56,s4110,{"s3110":84,"t25":92,"p80":19},s3210,{"p65":54,"s8510":45},{"t73":78,"s6210":11},{"s2110":98,"p11":16},{"p61":55,"t88":75},p38

非常感谢您抽出宝贵时间。对于我不成熟的问题,我很抱歉。

【问题讨论】:

    标签: regex google-apps-script split google-sheets


    【解决方案1】:

    B1:

    =ARRAYFORMULA(REGEXEXTRACT(A1:A5&",",REGEXREPLACE(REGEXREPLACE(A1:A5&",","{.*?}","($0)"),"([A-Za-z]\d+),","($1),")))
    

    我们用() 将除逗号, 之外的所有对象和字符串括起来,然后再将它们提取出来。

    编辑:更简单的锚点:, 逗号后面不跟"

    =ARRAYFORMULA(SUBSTITUTE(SPLIT(SUBSTITUTE(A1:A5,","&CHAR(34),"?"),","),"?",","&CHAR(34)))
    
    
    =ARRAYFORMULA(split(REGEXREPLACE(A1:A5,"(,)([^"&CHAR(34)&"])","?$2"),"?"))
    

    【讨论】:

    • 感谢您的回答。您的回答解决了我的问题。我不知道为此使用电子表格功能。我现在赞成。顺便说一句,我该怎么做才能接受 2 个答案?
    【解决方案2】:

    您可以使用 RegExp 将字符替换为可以在不影响其他任何内容的情况下找到的独特字符。

    function myFunction() {
      var L,newArray,thisElement;
    
      var myStrng = '{"p0":70,"u3":71,"s7110":40},t45,{"t78":60,"s3310":15},p37,p36,{"p29":44,"s8110":95},p85,p14,{"s2710":41},u47 \
    s6610,{"t25":70,"u8":43,"p35":86},u85,u74,{"s7710":83},{"p70":70,"u67":84},{"u71":43,"s1210":73},{"u45":84,"s710":15},{"u14":79,"p22":45},p31 \
    u73,u12,{"t51":98,"u57":96},u31,p41,s1110,s6610,p55,{"t57":71,"s7510":83,"u62":17},u73 \
    t50,{"t83":22,"p18":76},{"p47":12,"s8710":18,"u11":35},{"t14":74,"u72":51},{"p74":21,"t77":77},{"u62":84,"s3010":11},p81,u36,p67,{"t79":12,"u2":70,"s6010":98} \
    {"u54":51,"t31":31},t56,s4110,{"s3110":84,"t25":92,"p80":19},s3210,{"p65":54,"s8510":45},{"t73":78,"s6210":11},{"s2110":98,"p11":16},{"p61":55,"t88":75},p38';
    
      var re = new RegExp("\},","g");
      var parsedObj = myStrng.replace(re,"}zq^");//Replace all }, characters with }zq^
    
      //Logger.log(parsedObj)
    
      parsedObj = parsedObj.replace(/,\{/g,"zq^{");//Replace all ,{ characters with zq^{
    
      //Logger.log(parsedObj)
      parsedObj = parsedObj.replace(/\}\{/g,"}zq^{");//Replace all back to back brackets
      parsedObj = parsedObj.replace(/\} \{/g,"}zq^{");//Replace all back to back brackets with a space between
    
      parsedObj = parsedObj.split("zq^");//split on zq^
    
      L = parsedObj.length;
    
      newArray = [];
    
      for (var i=0;i<L;i++) {
        thisElement = parsedObj[i];
        //Logger.log('thisElement: ' + thisElement)
    
        if (thisElement.indexOf("{") !== -1) {
          newArray.push(thisElement);
          continue;
        }
    
        if (thisElement.indexOf(",") !== -1) {
          thisElement = thisElement.split(",");
    
          for (var j =0;j<thisElement.length;j++) {
            newArray.push(thisElement[j]);
          }
          continue;
        }
    
        if (thisElement.indexOf(" ") !== -1) {
          thisElement = thisElement.split(" ");
    
          for (var j =0;j<thisElement.length;j++) {
            newArray.push(thisElement[j]);
          }
          continue;
        }
    
        newArray.push(thisElement);
      }
    
      L = newArray.length;
    
      for (var i=0;i<L;i++) {
        Logger.log(newArray[i])
    
      }
    }
    

    【讨论】:

    • 感谢您的回答。您的回答解决了我的问题。我现在投了赞成票。顺便说一句,我该怎么做才能接受 2 个答案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多