[英]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.