简体   繁体   English

如何使用应用程序脚本将值从一个子文件夹中的某些电子表格复制到另一电子表格

[英]How can I copy values from some spreadsheets that are in one subfolder to another spreadsheet with app script

I need to copy values ​​from several spreadsheets that are in subfolders, for that I investigated how to search files between folders and subfolders with a script like the following ... 我需要从子文件夹中的几个电子表格中复制值,为此我研究了如何使用以下脚本在文件夹和子文件夹之间搜索文件...

function listFolders(folder) {

folder = folder || DriveApp.getRootFolder();

var name = folder.getName();

var files = folder.getFiles();

while ( files.hasNext() ) {
  Logger.log(name + " :: " + files.next().getName());
}

var subfolders = folder.getFolders();

while (subfolders.hasNext()) {
  listFolders(subfolders.next());
}

Until here it makes the search of between folders and subfolders of all the files and then I try to realize a condition which if it finds the folder "measurement", then copy from the files that there are some data to another spreadsheet, but it does not realize the condition and I do not know why... 直到这里,它都在所有文件的文件夹和子文件夹之间进行搜索,然后我尝试实现一种条件,即如果它找到文件夹“ measurement”,然后从文件中复制一些数据到另一个电子表格,但是它确实不知道情况,我不知道为什么...

   if( folder == "Measurement"){
   while (files.hasNext()) {
   var file = files.next();
   var bring = SpreadsheetApp.openById(file.getId());

   var srceSheet = bring.getSheets()[0];

    var lastRow = srceSheet.getLastRow();
    var range = srceSheet.getRange("A"+(lastRow)+":D"+(lastRow));
    var values = range.getValues();


    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var SSsheet = ss.getSheetByName("Sheet 1");
    var getRow = SSsheet.getLastRow() + 1;
    var ssRange = SSsheet.getRange(getRow, 2, values.length,
    values[0].length);
    ssRange.setValues(values);


  }
 }
}  

I do not know what I can have wrong, if the condition is badly declared or another way I investigate is to put the name of the files with this condition 'fullText contains "hello world"' or this other 'name contains "hello" and name contains "goodbye"' Declaring beforehand a variable that contains this as... var file = DriveApp.getRootFolder().searchFiles('name contains "hello" and name contains "goodbye"') . 我不知道我可能有什么问题,如果条件声明不正确,或者我研究的另一种方法是将具有此条件的文件名放入'fullText contains "hello world"'或该其他'name contains "hello" and name contains "goodbye"'预先声明一个包含该变量的变量,为... var file = DriveApp.getRootFolder().searchFiles('name contains "hello" and name contains "goodbye"')

Thanks for helping me, I was able to solve my question, investigate a little more and I found this method file.list of this page Developers.Google and my script was like this ... 感谢您的帮助,我得以解决我的问题,进行了进一步的研究,发现此方法file.list为该页面的file.list Developers.Google和我的脚本是这样的...

function SearchFiles() {
var searchFor ='fullText contains "Test"';
var names =[];
var fileIds=[];
var files = DriveApp.searchFiles(searchFor);
while (files.hasNext()) {
  var file = files.next();
  var fileId = file.getId();
  fileIds.push(fileId);
  var name = file.getName();
  names.push(name);

  var bring = SpreadsheetApp.openById(file.getId()); 
  var dePr = bring.getSheetByName("Sheet1 ");
  var veEq = bring.getSheetByName("Sheet2 ");


    var ltRowDe = dePr.getLastRow();
    var rgDe    = dePr.getRange("B"+(ltRowDe)+":E"+(ltRowDe));
    var valDe   = rgDe.getValues();


    var ltRowVe  = veEq.getLastRow();
    var rgVe    = veEq.getRange("O"+(ltRowVe)+":P"+(ltRowVe));
    var valVe    = rgVe.getValues();


    var ss = SpreadsheetApp.getActiveSpreadsheet();

    var ssDe = ss.getSheetByName("Sheet  1");
    var getRowDe = ssDe.getLastRow() + 1;
    var ssRgDe = ssDe.getRange(getRowDe, 5, valDe.length, valDe[0].length).setValues(valDe);  


    var ssVe = ss.getSheetByName("Sheet 2");
    var getRowVe = ssVe.getLastRow() + 1;
    var ssRgVe = ssVe.getRange(getRowVe, 7, valVe.length, valVe[0].length).setValues(valVe); 

}  
}  

Basically what my script does is that it performs a search by the drive of all the files that are called Test and it brings me the last data of the sheets that you specify of all those files to another spreadsheet. 基本上,我的脚本所做的是,它通过驱动器搜索称为Test的所有文件,并将我为所有这些文件指定的工作表的最后数据带到另一个电子表格。 Thanks for the help and I hope that some day someone will serve this script. 感谢您的帮助,我希望有一天有人会提供此脚本。 =D = d

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? 我可以将数据从一个电子表格复制到新的电子表格吗? - Can I copy data from one spreadsheet to a new spreadsheet? Google电子表格将值从一行复制到另一张工作表 - Google spreadsheet copy values from one row to another sheet Google Spreadsheets 脚本 - 如何将范围从一张纸复制到另一张纸,但不覆盖非空目标单元格 - Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells 如何在应用脚本中将一个 script.gs 中的变量调用到另一个 script.gs - How can i call a variable from one script.gs to another script.gs in app script 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 如何使用谷歌表格中的脚本将图像从一个单元格复制到另一个单元格? - How can I use a script in google sheets to copy an image from one cell to another? 如何在编辑时将数据从一个电子表格复制到另一个电子表格 - How to copy data from one spreadsheet to another on edit 如何将HTML文本框值从一个域复制到另一个域的文本框? - How can I copy HTML textbox values from one domain to another domain's textboxes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM