为什么我无法使用FileIterator从文件中获取数据?

时间:2015-05-07 17:22:41

标签: google-apps-script

我正在使用此代码...它在我引用的文件夹中返回正确的页数,但只返回" undefined"而不是每张表中的值。

function onOpen() {
 var folder = DriveApp.getFolderById("paosdfpoasidjfosidj");
var file;
var data;

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Base")
sheet.clearContents();

var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12;
 if (file.getMimeType=="GOOGLE_SHEETS")
 {
    var otherSheet=  SpreadsheetApp.open(file).getSheetByName("Project Overview");

  value1 = otherSheet.getRange('B4').getValue();
  value2 = otherSheet.getRange('B5').getValue();
  value3 = otherSheet.getRange('B3').getValue();
  value4 = otherSheet.getRange('B7').getValue();
  value5 = otherSheet.getRange('B8').getValue();
  value6 = otherSheet.getRange('B11').getValue();
  value7 = otherSheet.getRange('B6').getValue();
  value8 = otherSheet.getRange('B18').getValue();
  value9 = otherSheet.getRange('I4').getValue();
  value10 = otherSheet.getRange('I5').getValue();
  value11 = otherSheet.getRange('I6').getValue();
  value12 = otherSheet.getRange('I3').getValue();
var url = file.getUrl(); 

}
   else {
  value1 = null; value2 = null; value3 = null;value4 = null; value5 = null; value6 = null;value7 = null; value8 = null; value9 = null;value10 = null; value11 = null; value12 = null;value13 = null; value14 = null; value15 = null;value16 = null; value17 = null; value18 = null;value19 = null; value20 = null; value21 = null;value22 = null; value23 = null; value24 = null;value25 = null; value26 = null; value27 = null;value28 = null; value29 = null; value30 = null;value31 = null; value32 = null; value33 = null;value34 = null; value35 = null; value36 = null;
 }




  sheet.appendRow([url,value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11, value12]);

}

1 个答案:

答案 0 :(得分:0)

问题是语法之一。我也切换并使用了一个数组,这使得工作更快,并且没有经常调用.getRange。这是工作代码:

function onOpen() {
var folder = DriveApp.getFolderById("09834j5209834nh57987");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Base")
sheet.clearContents();



 var files = folder.getFiles();
 while (files.hasNext()) {
 var file = files.next();


    var otherSheet= SpreadsheetApp.open(file).getSheetByName("Project Overview");

 var data = otherSheet.getRange("B3:I20").getValues().map(function(row){ return row[0];});

 var value1 = data[1,1].valueOf();
 var value2 = data[1,2].valueOf();
 var value3 = data[0,0].valueOf();




var url = file.getUrl(); 
var name = "Link to Sheet"; 
var semi = ";";  
var hyp = "=hyperlink(\"";
var quot = "\"";
var clos = ")";
var pp = hyp  + url + quot + semi + quot + name + quot + clos;



  sheet.appendRow([pp,value1, value2, value3, ]);
 }}