无论如何,是否可以循环浏览Google云端硬盘中特定文件夹中的所有电子表格?

时间:2019-06-20 01:18:36

标签: google-apps-script google-sheets

现在,我有了一个可以遍历电子表格中所有工作表的脚本。我的文件夹中有很多电子表格。如何在包含多个电子表格的Google云端硬盘文件夹中循环浏览所有电子表格

我试图在一个电子表格中循环浏览所有活动工作表,但它确实有效,但是我不知道如何在一个文件夹中循环浏览所有包含一张工作表的电子表格

每个文件夹之间的差异是月份

 function checkSales(){

 var app = SpreadsheetApp;
 var activeSheet = app.getActiveSpreadsheet().getSheets();
 //var data=activeSheet.getDataRange().getValues();

 //loop through sheets to look for value
 for (var i in activeSheet) {

 SpreadsheetApp.setActiveSheet(activeSheet[i])
 var sheet = app.getActiveSheet();
 var data = activeSheet[i].getDataRange().getValues();

 var emailAddress=SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName("Battery").getRange("H26").getValue();
 var resultArr=[];
 var xTitle = 'Part Numbers'; // XAxis Title
 var yTitle = 'Quantity'; // YAxis Title
 var column = sheet.getRange("A1:A22");
 column.setNumberFormat("@");

 //To Loop through the whole data Rows
 for(var i=1;i<data.length;i++)
 {
    //Takes columns from L to S (To loop through the Columns)
    for(var j=11;j<19;j++)
     {
       var cellVal=data[i][j];
       Logger.log(cellVal)
       if(cellVal>0)
       {
          //Stores the Part No, Month Header Value of the Column, Cell 
          Value which is greater then 0
          resultArr.push([data[i][0],data[0][j],cellVal])
        }
      }
   }
    if(resultArr.length>0)
       {
          var subject = 'Range exceeded Alert' + "" + sheet.getName();

          //Creates a body through the obtained values
          var body='';
          for(var m=0;m<resultArr.length;m++)
           {
            body+="For Part No "+resultArr[m][0].toString()+" and Month 
           "+resultArr[m][1]
           .toString()+", Value is "+resultArr[m][2].toString()+"<br>";
           }

       }

      }

   }

1 个答案:

答案 0 :(得分:2)

您可以使用此功能遍历文件

function checkSales(){
  var file, files = DriveApp.getFolderById(*****id).getFiles(); 
  //put the id of your target folder in the getFolderById()
  while (files.hasNext()) {
    file = files.next();
   var activeSpreadSheet = SpreadsheetApp.open(file);
    var sheets = activeSpreadSheet.getSheets();

  //loop through sheets to look for value
  for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) {


  var sheet = sheets[sheetIndex]
  var data = sheet.getDataRange().getValues();

  var emailAddress=activeSpreadSheet.getSheetByName("Battery").getRange("H26").getValue();

/* I edited until here, did not touch code below */

  var resultArr=[];
  var xTitle = 'Part Numbers'; // XAxis Title
  var yTitle = 'Quantity'; // YAxis Title
  var column = sheet.getRange("A1:A22");
  column.setNumberFormat("@");

  //To Loop through the whole data Rows
  for(var i=1;i<data.length;i++)
  {
   //Takes columns from L to S (To loop through the Columns)
     for(var j=11;j<19;j++)
     {
      var cellVal=data[i][j];
      Logger.log(cellVal)
      if(cellVal>0)
      {
        //Stores the Part No, Month Header Value of the Column, Cell 
        Value which is greater then 0
        resultArr.push([data[i][0],data[0][j],cellVal])
      }
     }
   }
   if(resultArr.length>0)
   {
      var subject = 'Range exceeded Alert' + "" + sheet.getName();

      //Creates a body through the obtained values
      var body='';
      for(var m=0;m<resultArr.length;m++)
       {
        body+="For Part No "+resultArr[m][0].toString()+" and Month 
       "+resultArr[m][1]
       .toString()+", Value is "+resultArr[m][2].toString()+"<br>";
       }

   }
  }

  }

 }