导入CSV期间超过了最长执行时间

导入CSV期间超过了最长执行时间

本文介绍了导入CSV期间超过了最长执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了Google的如何编写导入CSV文件的脚本。经过很多的帮助,我得到了它的工作。但是,当我尝试复制它或以任何方式修改它时,它会显示错误消息:
$ b

...并且无法运行。为什么会发生这种情况?



这是教程中的 importFromCSV()函数:

 函数importFromCSV(){
var fileName = Browser.inputBox(在文档列表中输入要导入的文件的名称(例如myFile.csv):);

var files = DocsList.getFiles();
var csvFile =; (文件[i] .getName()==文件名){
csvFile = {

for(var i = 0; i< files.length; i ++)文件[I] .getContentAsString();
休息;
}
}
var csvData = CSVToArray(csvFile,,);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
for(var i = 0; i< csvData.length; i ++){
sheet.getRange(i + 1,1,1,csvData [i] .length).setValues(new Array( csvData [I]));
}
}


解决方案

CSV导入示例为每行调用 CSVToArray

查看。第一条指南是减少对其他服务的呼叫;将示例更改为更新数组的所有行,然后调用 var arrData = [[]] 一次,这样可以大大提高性能。



替换为:

  for(var i = 0; i< csvData.length ; i ++){
sheet.getRange(i + 1,1,1,csvData [i] .length).setValues(new Array(csvData [i]));

$ / code


$ b



  sheet.clear(); 

var numRows = csvData.length;
var numCols = csvData [0] .length; //假设所有行的宽度都是相同的

//对电子表格API进行一次调用以将值写入表单。
sheet.getRange(1,1,numRows,numCols).setValues(csvData);
SpreadsheetApp.flush();

我还发现我需要在 var arrData中更改几行= new Array(),而不是像这样声明数组: setValues(),我用这个: setValues 。它不应该(在JavaScript中),但它(在Google Apps脚本中)。


I followed Google's Interacting With Your Docs List tutorial on how to write a script on importing CSV files. After a lot of help I got it to work. However when I try to copy it, or modify it in any way, it times out with the error message:

...and doesn't run. Why would this be happening?

This is the importFromCSV() function from the tutorial:

function importFromCSV() {
  var fileName = Browser.inputBox("Enter the name of the file in your Docs List to import (e.g. myFile.csv):");

 var files = DocsList.getFiles();
  var csvFile = "";

  for (var i = 0; i < files.length; i++) {
    if (files[i].getName() == fileName) {
      csvFile = files[i].getContentAsString();
      break;
    }
  }
  var csvData = CSVToArray(csvFile, ",");
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  for (var i = 0; i < csvData.length; i++) {
    sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
  }
}
解决方案

The CSV import example does a CSVToArray call for every row. That's slow.

Check out the advice in the Best Practices Doc. The first piece of guidance is to reduce calls to other services; change the example to update all rows of an array, and then call var arrData = [[]] just once, and you will vastly improve the performance.

Replace this:

for (var i = 0; i < csvData.length; i++) {
  sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
}

With this:

sheet.clear();

var numRows = csvData.length;
var numCols = csvData[0].length; // assume all rows are same width

// Make a single call to spreadsheet API to write values to sheet.
sheet.getRange(1, 1, numRows, numCols).setValues( csvData );
SpreadsheetApp.flush();

I also found that I needed to change a couple of lines in var arrData = new Array(), instead of declaring the arrays like this: setValues(), I used this: setValues. It shouldn't matter (in JavaScript), but it did (in Google Apps Script).

这篇关于导入CSV期间超过了最长执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 18:31