有没有办法通过脚本通过固定宽度拆分列?尝试解析数据

时间:2019-06-18 20:48:39

标签: google-apps-script google-sheets

Here is a link to a sample spreadsheet with 2 tabs, one for my data output and another for the desired result

我要将文本数据导入到表格中,并且需要通过定义的固定宽度将其拆分。我知道列宽限制,这是我试图定义列中断的地方,因为数据不会比每列的宽,但是它没有一致的定界符。

我以前在Excel中使用以下VBA代码完成了此操作:

Workbooks.OpenText Filename:=sFileName, Origin:=437, StartRow:=41, 
DataType:=xlFixedWidth, FieldInfo:=Array( _
    Array(0, 1), _
    Array(23, 1), _
    Array(34, 1), _
    Array(59, 1), _
    Array(70, 1), _
    Array(79, 1), _
    Array(87, 1), _
    Array(98, 1), _
    Array(114, 1), _
    Array(123, 1)), _
    TrailingMinusNumbers:=True

我已经设置了将文本文件拉入工作簿的代码,我尝试使用SLICE函数进行拆分,但是它给出了一个错误,即该范围中的数据约有4000行,因此有0行,因此它无法生成。

function importTXTfromDrive() {

  var fileName = Browser.inputBox("Enter the name of the text file in your 
Google Drive to import (e.g. myFile.csv):");

  var searchTerm = "title = '"+fileName+"'";

  var files = DriveApp.searchFiles(searchTerm)
  var csvFile = "";


  while (files.hasNext()) {
    var file = files.next();
    if (file.getName() == fileName) {
        csvFile = file.getBlob().getDataAsString();
    break;
    }
  }

  var csvData = Utilities.parseCsv(csvFile);
  ////var col1 = csvData.slice(23,1);
  ////This didn't work so I removed it for now
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  if (ss.getSheetByName(fileName) != null){
   Browser.msgBox("That file has already been opened");
    return 0;
  }

  var create = ss.insertSheet(fileName);

  create.getRange(1, 1, csvData.length, 
csvData[0].length).setValues(csvData); 

////create.getRange(1, 1, csvData.length, 
////csvData[0].length).setValues(col1); 
////This last line was a test in using the SLICE but gave an error  
}

2 个答案:

答案 0 :(得分:1)

这是一个起点。可能会进行一些修整,也可能会将字符串转换为数字,但这涵盖了基本的解析。

  useEffect(() => {
     fetch('some_url).then(res => res.json()).then(books => setBookList(books)); 
  }, []);

之前(一列)

Screenshot-BEFORE

之后(十列)

Screenshot-after

答案 1 :(得分:0)

例如拆分{ "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", "actions": { "Post_message": { "inputs": { "host": { "connection": { "name": "@parameters('$connections')['slack']['connectionId']" } }, "method": "post", "path": "/chat.postMessage", "queries": { "channel": "C0N******UT", "text": "Hello there!" } }, "runAfter": {}, "type": "ApiConnection" } }, "contentVersion": "1.0.0.0", "outputs": {}, "parameters": { "$connections": { "defaultValue": {}, "type": "Object" } }, "triggers": { "manual": { "inputs": { "schema": {} }, "kind": "Http", "type": "Request" } } }, "parameters": { "$connections": { "value": { "slack": { "connectionId": "/subscriptions/b8*******23f/resourceGroups/RG_NAME/providers/Microsoft.Web/connections/slack", "connectionName": "slack", "id": "/subscriptions/b83c1ed************4c23f/providers/Microsoft.Web/locations/westus2/managedApis/slack" } } } } } 分成4列,分别为宽度20、30、40和剩余宽度,请使用:

A1:A10

使用此公式,将修剪所有开头和结尾的空白,并将所有预期格式的字符串都转换为相应的数字,日期等。 =SPLIT(ARRAYFORMULA(REGEXREPLACE(TO_TEXT(A1:A10), "\A(.{1,"&JOIN("})?(.{1,",{20,30,40})&"})?(.*)\z", "$"&JOIN("<tab>$",SEQUENCE(COLUMNS({20,30,40})+1)))), "<tab>",0,0) 会变成00000123,而123会变成当年的1月1日,或2020年变成1/1

如果您需要将所有结果值设置为文本格式,请使用以下方法:

43862

=ARRAYFORMULA(REGEXREPLACE(SPLIT(REGEXREPLACE(TO_TEXT(A1:A10), "\A(.{1,"&JOIN("})?(.{1,",{20,30,40})&"})?(.*)\z", "$"&JOIN("<as_text><tab><as_text>$",SEQUENCE(COLUMNS({20,30,40})+1))), "<tab>",0,0),"<as_text>","")) <tab>在这里用作唯一字符串 在您的文本中不出现 ,因此,如果其中包含它们,则需要使用不同的东西,例如<as_text><column_break>