异常:数据中的列数与范围内的列数不匹配。 - 问题

发布于 2025-02-13 06:24:06 字数 1847 浏览 3 评论 0原文

    function pricing() {
      convert_txt_gsheets();
      lastrow2();
    }
    
    function convert_txt_gsheets() {
      var source = SpreadsheetApp.openById('ID1').getSheetByName('Daily Report');
      var targetrange = source.getRange(2, 1, source.getLastRow(), source.getLastColumn());
      targetrange.clear();
    
      var file = DriveApp.getFileById('ID2');
      var body = file.getBlob().getDataAsString().split(/\r/);
      var result = body.map(split(/|/))
        // vvv
        .map(row => row.map(cell => cell.replaceAll(`"`, ``)));
      SpreadsheetApp.getActive().getSheetByName('Daily Report').getRange(1, 1, result.length, result[0].length).setValues(result);
      return;
    }



function lastrow2() {
var source = SpreadsheetApp.openById('ID1').getSheetByName('Daily Report');
var target = SpreadsheetApp.openById('ID1').getSheetByName('Permanent Record');
var target = target.getRange(target.getLastRow()+1, 1, source.getLastRow(), source.getLastColumn());
var rangeValues = source.getRange(2, 1, source.getLastRow(), source.getLastColumn()).getValues();
target.setValues(rangeValues); 
}

我可以在上述代码方面寻求帮助吗?功能Lastrows2正常工作。但是,convert_txt_gsheets抛出异常“异常:数据中的列数与范围内的列数不匹配。数据具有1个,但范围有9个。Convert_TXT_GSHEETS“终止整个内容,并停止Lastrow2,并从Evers中停止Lastrow2。参与。

这是从TXT提取的示例数据。

Column0Column1Column2 Column3Column4Column5 Column6Column6Column7Column7
Rocketship”“ 5.99”“ 5.39”“ 5.39”“”“”“ 5.39”5.39”“ 5.39”“ 7.5.2022”“ William”

编辑我修改了示例和原始代码。当前收到的错误是

参考文献:未定义拆分

    function pricing() {
      convert_txt_gsheets();
      lastrow2();
    }
    
    function convert_txt_gsheets() {
      var source = SpreadsheetApp.openById('ID1').getSheetByName('Daily Report');
      var targetrange = source.getRange(2, 1, source.getLastRow(), source.getLastColumn());
      targetrange.clear();
    
      var file = DriveApp.getFileById('ID2');
      var body = file.getBlob().getDataAsString().split(/\r/);
      var result = body.map(split(/|/))
        // vvv
        .map(row => row.map(cell => cell.replaceAll(`"`, ``)));
      SpreadsheetApp.getActive().getSheetByName('Daily Report').getRange(1, 1, result.length, result[0].length).setValues(result);
      return;
    }



function lastrow2() {
var source = SpreadsheetApp.openById('ID1').getSheetByName('Daily Report');
var target = SpreadsheetApp.openById('ID1').getSheetByName('Permanent Record');
var target = target.getRange(target.getLastRow()+1, 1, source.getLastRow(), source.getLastColumn());
var rangeValues = source.getRange(2, 1, source.getLastRow(), source.getLastColumn()).getValues();
target.setValues(rangeValues); 
}

Could I ask for help with the above code? The function Lastrows2 works properly. However, the Convert_txt_gsheets throws exception "Exception: The number of columns in the data does not match the number of columns in the range. The data has 1 but the range has 9. convert_txt_gsheets" which terminates the whole thing, and stops lastrow2 from ever engaging.

This is the example data that is being pulled from txt.

COLUMN0COLUMN1COLUMN2COLUMN3COLUMN4COLUMN5COLUMN6COLUMN7COLUMN8
"Rocketship""5.99""5.39""5.39""""5.39""5.39""7.5.2022""william"

Edit I modified the example and the original code. The error currently received is

ReferenceError: split is not defined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2025-02-20 06:24:07

您几乎很好,问题在这里:

  var result = body.map(r => r.split(/,/))
    // vvv
    .map(row => row.map(cell => cell.replaceAll(", ``)));

这里我所做和测试的功能:

function cleantext() {

  var text = `COLUMN0,COLUMN1,COLUMN2,COLUMN3,COLUMN4,COLUMN5,COLUMN6,COLUMN7,COLUMN8
"Rocketship","5.99","5.39","5.39","","5.39","5.39","7.5.2022","william"
"WobblyHouse","3.99","3.49","3.49","","3.49","3.49","7.5.2022","billiam"`;

  var cleaned = text.split('\n').map(line => line.split(',').map(cell => cell.replace(/["]/g, "")));

  Logger.log(cleaned);
}

// [[COLUMN0, COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5, COLUMN6, COLUMN7, COLUMN8], 
// [Rocketship, 5.99, 5.39, 5.39, , 5.39, 5.39, 7.5.2022, william], 
// [WobblyHouse, 3.99, 3.49, 3.49, , 3.49, 3.49, 7.5.2022, billiam]]

You are almost good, the problem is here :

  var result = body.map(r => r.split(/,/))
    // vvv
    .map(row => row.map(cell => cell.replaceAll(", ``)));

Here the function I did and tested :

function cleantext() {

  var text = `COLUMN0,COLUMN1,COLUMN2,COLUMN3,COLUMN4,COLUMN5,COLUMN6,COLUMN7,COLUMN8
"Rocketship","5.99","5.39","5.39","","5.39","5.39","7.5.2022","william"
"WobblyHouse","3.99","3.49","3.49","","3.49","3.49","7.5.2022","billiam"`;

  var cleaned = text.split('\n').map(line => line.split(',').map(cell => cell.replace(/["]/g, "")));

  Logger.log(cleaned);
}

// [[COLUMN0, COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5, COLUMN6, COLUMN7, COLUMN8], 
// [Rocketship, 5.99, 5.39, 5.39, , 5.39, 5.39, 7.5.2022, william], 
// [WobblyHouse, 3.99, 3.49, 3.49, , 3.49, 3.49, 7.5.2022, billiam]]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文