参考:未定义

发布于 2025-02-13 21:39:41 字数 2568 浏览 1 评论 0原文

功能lastrows2正常工作。但是,convert_txt_gsheets投掷

参考文献:未定义拆分

哪个终止整个过程,并阻止Lastrow2参与。分裂使我非常困惑。

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); 
}

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

COLUMN0|COLUMN1|COLUMN2|COLUMN3|COLUMN4|COLUMN5|COLUMN6|COLUMN7|COLUMN8  
"Rocketship"|"5.99"|"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');
let body = file.getBlob().getDataAsString().split('\r'); //  split into rows
let results = body.map( row => row.split('|') ); // split into columns
results = result.map( col => col.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); 
}

The function Lastrows2 works properly. However, the Convert_txt_gsheets throws

ReferenceError: split is not defined

which terminates the whole thing, and stops lastrow2 from ever engaging. Splits confuse me immensely.

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); 
}

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

COLUMN0|COLUMN1|COLUMN2|COLUMN3|COLUMN4|COLUMN5|COLUMN6|COLUMN7|COLUMN8  
"Rocketship"|"5.99"|"5.39"|"5.39"|""|"5.39"|"5.39"|"7.5.2022"|"william"

MODIFIED CODE

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');
let body = file.getBlob().getDataAsString().split('\r'); //  split into rows
let results = body.map( row => row.split('|') ); // split into columns
results = result.map( col => col.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); 
}

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

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

发布评论

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

评论(1

国产ˉ祖宗 2025-02-20 21:39:42

您在var result = body.map(split(/|/))中犯了一个错误。尝试一下。

替换:

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

这是我的测试集。适当地更换上面的行。

function test() {
  try {
    let data = 'COLUMN0|COLUMN1|COLUMN2|COLUMN3|COLUMN4|COLUMN5|COLUMN6|COLUMN7|COLUMN8\r "Rocketship"|"5.99"|"5.39"|"5.39"|""|"5.39"|"5.39"|"7.5.2022"|"william"';
    let body = data.split('\r'); //  split into rows
    let results = body.map( row => row.split('|') ); // split into columns
    results = results.map( col => col.map( cell => cell.replaceAll(`"`, ``) ) );
    console.log(results);
  }
  catch(err) {
    console.log(err);
  }
}

执行日志

我不确定日期,但是当您setValues()()时,其他人会转换为适当的类型。

6:59:01 AM  Notice  Execution started
6:59:04 AM  Info    [ [ 'COLUMN0', 'COLUMN1', 'COLUMN2', 'COLUMN3', 'COLUMN4', 'COLUMN5', 'COLUMN6', 'COLUMN7', 'COLUMN8' ],
  [ ' Rocketship', '5.99', '5.39', '5.39', '', '5.39', '5.39',  '7.5.2022', 'william' ] ]
6:59:02 AM  Notice  Execution completed

You made a mistake in var result = body.map(split(/|/)). Try this.

Replace:

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

This is my test set. Replace the lines above as appropriate.

function test() {
  try {
    let data = 'COLUMN0|COLUMN1|COLUMN2|COLUMN3|COLUMN4|COLUMN5|COLUMN6|COLUMN7|COLUMN8\r "Rocketship"|"5.99"|"5.39"|"5.39"|""|"5.39"|"5.39"|"7.5.2022"|"william"';
    let body = data.split('\r'); //  split into rows
    let results = body.map( row => row.split('|') ); // split into columns
    results = results.map( col => col.map( cell => cell.replaceAll(`"`, ``) ) );
    console.log(results);
  }
  catch(err) {
    console.log(err);
  }
}

Execution log

I'm not sure about the date but the others will be converted to their appropriate type when you setValues().

6:59:01 AM  Notice  Execution started
6:59:04 AM  Info    [ [ 'COLUMN0', 'COLUMN1', 'COLUMN2', 'COLUMN3', 'COLUMN4', 'COLUMN5', 'COLUMN6', 'COLUMN7', 'COLUMN8' ],
  [ ' Rocketship', '5.99', '5.39', '5.39', '', '5.39', '5.39',  '7.5.2022', 'william' ] ]
6:59:02 AM  Notice  Execution completed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文