有没有办法 运行 编码到 "certain point",退出,然后不间断地再次开始 运行ning

Is there a way to run code up to a "certain point", quit, and then start running again without an interruption

我的脚本从 CSV 文件中获取数据,然后将其导出到 Google Sheet。 CSV 文件由一个二维数组组成,有 9000 多行。我当前的脚本 运行 没问题,但出现

错误

Incorrect Range Width, was 1 but should be 5

仅当我处理整个 CSV 文件时才会出现此错误,而如果我将文件分成多个块,则使用相同的代码可以正常处理而不会出现任何错误。因此,错误与行数有关。我使用它超时的循环功能。

我想知道是否有办法 运行 处理 2000 行的代码退出然后重新开始而不会受到任何中断。我已经被这个错误困扰了几个星期,真的需要帮助。谢谢

这是我的代码:

Function getCSV() {
var fSource = DriveApp.getFolderById('0B2lVvlNIDosoajRRMUwySVBPNVE');        //reports_folder_id = id of folder where csv reports are saved 
var date= Utilities.formatDate(new Date(), "GMT", "dd-MM-yy");
var fi = fSource.getFilesByName('L661_BOM-CAD_07-01-16.csv'); 
// latest  report file
var ss =   SpreadsheetApp.openById('1V8YG8lyNZiTllEPHENcnabYRLDPCK6mHGUyAyNhW0Is').getSheet    s()[0]; // data_sheet_id = id of spreadsheet that holds the data to be  updated  with new report data Sheet will be opened server side. 

ss.getName() == "Sheet1"
if ( fi.hasNext()) { // proceed if "report.csv" file exists in the reports    folder
var file = fi.next();
//file.setName('L661_BOM-CAD_'+ date +'(EXPORTED)'+'.csv');
var csv = file.getBlob().getDataAsString();
var csvData = CSVToArray(csv);
Logger.log('csvData[0].length: ' + csvData[0].length + ' csvData.length:'  + csvData.length);
var lastrow = ss.getLastRow();

ss.getRange(lastrow +  1,1,csvData.length,csvData[0].length).setValues((csvData));
}

//adds the last modified date to the first row
if( ss.getName() == "Sheet1" ) { //checks that we're on the correct sheet
var r= ss.getRange('A1');
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 5);
if( nextCell.getValue() === '' ) //is empty?
var date = new Date();
var date= Utilities.formatDate(new Date(), "GMT", "dd-MM-yy");
nextCell.setValue(date); //enters the date in F1 in dd/mm/yyyy format
 };
 };
};

function CSVToArray( strData, strDelimiter ){

strDelimiter = (strDelimiter || ';');

var objPattern = new RegExp(
    (
        // Delimiters.
        "(\" + strDelimiter + "|\r?\n|\r|^)" +

        // Quoted fields.
        "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

        // Standard fields.
        "([^\"\" + strDelimiter + "\r\n]*))"
    ),
    "gi"
    );


var arrData = [[]];


var arrMatches = null;

while (arrMatches = objPattern.exec( strData )){

    // Get the delimiter that was found.
    var strMatchedDelimiter = arrMatches[ 1 ];

    // Check to see if the given delimiter has a length
    // (is not the start of string) and if it matches
    // field delimiter. If id does not, then we know
    // that this delimiter is a row delimiter.
    if (
        strMatchedDelimiter.length &&
        strMatchedDelimiter !== strDelimiter
        ){

        // Since we have reached a new row of data,
        // add an empty row to our data array.
        arrData.push( [] );

    }

    var strMatchedValue;

    // Now that we have our delimiter out of the way,
    // let's check to see which kind of value we
    // captured (quoted or unquoted).
    if (arrMatches[ 2 ]){

        // We found a quoted value. When we capture
        // this value, unescape any double quotes.
        strMatchedValue = arrMatches[ 2 ].replace(
            new RegExp( "\"\"", "g" ),
            "\""
            );

    } else {

        // We found a non-quoted value.
        strMatchedValue = arrMatches[ 3 ];

    }

    // Now that we have our value string, let's add
    // it to the data array.
    arrData[ arrData.length - 1 ].push( strMatchedValue );
    Logger.log('arrData[0].length: ' + arrData[0].length);
}

// Return the parsed data.
return( arrData );
} 

我在你的代码中找不到遍历行的循环,但基本上你可以做类似的事情(你需要一个 var rowcount = 0 每行增加的值):

if (rowcount == 2000) {
    console.log("found row 2000");
}

然后在console.log()上设置断点。

我相信你的分隔符是分号。

这是导入和写入当前打开的方法的方法sheet

function getcsv(){

  // get the data from drive
  var csvString = DriveApp
    .getFileById('0B92ExLh4POiZTHFDUThmaHE2d2s1LVpvbWhyNjJaOE1MejBZ')
    .getBlob()
    .getDataAsString();

  // convert to array of arrays
  var csvData = Utilities.parseCsv(csvString, ';');

  // write to sheet
  SpreadsheetApp
    .getActiveSheet()
    .getRange(1,1,csvData.length, csvData[0].length)
    .setValues(csvData);


}