异常:数据中的列数与范围中的列数不匹配.——问题


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会抛出异常" exception:数据中的列数与范围中的列数不匹配。数据是1,但范围是9。convert_txt_gsheets"这会终止整个过程,并阻止lastw2参与。

这是从txt中提取的示例数据。

<表类>COLUMN0COLUMN1COLUMN2COLUMN3COLUMN4COLUMN5COLUMN6COLUMN7COLUMN8tbody><<tr>"Rocketship""5.99">"5.39">"5.39">">"5.39">"5.39">"7.5.2022""william"

你做得差不多了,问题在这里:

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]]

相关内容

  • 没有找到相关文章

最新更新