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

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) 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...


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"'). Thanks for read this and try to help me.


'fullText contains "hello world"'


'name contains "hello" and name contains "goodbye"'


var file = DriveApp.getRootFolder().searchFiles('name contains "hello" and name contains "goodbye"')





If the origin folder will always be "Measurement" then it's better to grab its ID and use DriveApp.getFolderById(id) rather than iterating over all folders. The other thing is that you variale folder is of the type Folder, so you can't compare it to the string "Measurement". You can change to if( folder.getName() == "Measurement")
– filipeglfw
Aug 21 at 18:34


DriveApp.getFolderById(id)


folder


Folder


if( folder.getName() == "Measurement")





Yes, always the source folder will be called Measurement, the problem is that it is not just a folder but many are called Measurement so I can not declare the method with the ID, thanks, you made me realize something that I let go, but for now it's still not working, I'll keep trying and I hope someone can guide me to solve this :D
– Luis Avl
Aug 21 at 20:10


Measurement


Measurement





This line of code: if( folder == "Measurement"){ If folder is not a string, then it will never be equal. You should have a variable named folderName and use that in your code. If folder is a folder object, then it will never equal the string "Measurement" I'm going to assume that a variable named folder is a folder object, and not the folder name. It seems like you need to thoroughly debug your code.
– Sandy Good
Aug 21 at 21:47



if( folder == "Measurement"){


folder


folderName


folder


folder




1 Answer
1



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


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. Thanks for the help and I hope that some day someone will serve this script. =D


Test






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)