I have read 10+ questions about deleting items in Javascripts loops but they don't seem to apply to my situation.
I have this code
var childFolders = parent.getFolders();
// List folders inside the folder
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
//processing childFolder
After this I want to delete the current item (childFolder) from the array childFolders. The reason is that I am doing some work in a Google Apps script but it often takes too long and times out so I need to be able to restart the loop and only have unprocessed items left in the array. To achieve this, in every loop, I copy the content of childFolders into a permanent storage that I can restore on the next run.
I believe your goal is as follows.
You want to stop the loop of the folder iterator, and when you run the script again, you want to start the folder iterator from the stopped iterator.
In this case, how about using "ContinuationToken"? When "ContinuationToken" is used, I thought that your goal might be able to be achieved by the native methods of Google Apps Script. When this is reflected in your script, how about the following script?
Sample script:
Please set var parent = DriveApp.getFolderById("###");.
// When you want to clear the token, please run this function.
function clearToken() {
PropertiesService.getScriptProperties().deleteProperty("token");
}
// This is the main function.
function main() {
var parent = DriveApp.getFolderById("###"); // Please set your parent folder.
var numberOfLoop = 2; // The folder iterator is run every this number.
var p = PropertiesService.getScriptProperties();
var token = p.getProperty("token");
var childFolders = token ? DriveApp.continueFolderIterator(token) : parent.getFolders();
var count = 0;
while (childFolders.hasNext()) {
count++;
var childFolder = childFolders.next();
//processing childFolder
console.log(childFolder.getName()); // This is a sample.
if (count == numberOfLoop) {
var token = childFolders.getContinuationToken();
p.setProperty("token", token);
break;
}
}
}
When you run main function, in this sample script, 2 folders are processed and the script is finished. When you run main again, the folder iterator is started from the continuation.
When you want to start from the 1st folder iterator, please run clearToken.
References:
getContinuationToken() of Class FolderIterator
continueFolderIterator(continuationToken) of Class DriveApp
Added:
From your following reply,
It is the script in step 3 here ourtechroom.com/fix/… I have problem with. I changed it to add all files to an array first and insert them into a sheet in a separate step at the end but that wasn't enough. Hence my question.
My issue is that your solution is a little too complicated for me. That is, I have a hard time applying your solution to the script in the link.
Do you want to retrieve the file metadata of all files in your Google Drive? If my understanding is correct, I think that the script in your link is a high process cost because appendRow is used in the loop. Ref I thought that this might be the reason for your actual situation. If my understanding is correct, how about the following sample script?
Usage:
1. Install Google Apps Script library.
You can see how to install Google Apps Script library of [FilesApp] at here.
2. Enable Drive API.
This modified script uses Drive API. So, please enable Drive API at Advanced Google services.
3. Sample script.
Please copy and paste the following script to the script editor of Spreadsheet. And, please set the top folder ID to folderId. If you use var folderId = "root";, all files in your Google Drive are retrieved.
function myFunction() {
var folderId = "###"; // Please set the top folder ID.
var header = ["parent", "folder", "name", "update", "Size", "URL", "ID", "description", "type"]; // This is from your script.
var obj = FilesApp.createTree(folderId, null, "files(name,modifiedTime,size,webViewLink,id,description,mimeType)");
var values = [header, ...obj.files.flatMap(({ folderTreeByName, filesInFolder }) => {
const f = [folderTreeByName.join("|"), folderTreeByName.pop()];
return filesInFolder.length == 0 ? [[...f, ...Array(7).fill(null)]] : filesInFolder.filter(({ mimeType }) => mimeType != MimeType.FOLDER).map(({ name, modifiedTime, size, webViewLink, id, description, mimeType }) => [...f, name || null, new Date(modifiedTime), size || 0, webViewLink, id, description || null, mimeType]);
})];
SpreadsheetApp.getActiveSheet().clear().getRange(1, 1, values.length, values[0].length).setValues(values);
}
References:
FilesApp of Google Apps Script library (Author me)
Files: list of Drive API v3
Related
I wrote the google-script. In that script I have a function that I need to use in 250+ different tables (google-sheets). Question is - How can I make it function accessible in each table?
To make a script or custom function available across other Google Sheets, publish it as a library.
Bruce Mcpherson has a nice post on how to do this: http://ramblings.mcpher.com/Home/excelquirks/gooscript/lib
Yes, it is possible.
Two options:
Create a Apps Script project that calls the 250+ spreadsheet, makes the operations and updates each spreadsheet which the results.
On this you'll find a collection of tutorials to start with apps script.
https://developers.google.com/apps-script/articles/tutorials
Create a function that you can call from any cell of your spreadsheet just as you call any other default function like SUM()
On this documentation you'll find explained how to do this second option.
https://developers.google.com/apps-script/guides/sheets/functions#arguments
Ok, I fond the solution. In my case files that I need to change is placed in deferent folders and have similar name
// function that i need to execute
function updatePrices(sheetId){
// do some staff
var activeSpreadsheet = SpreadsheetApp.openById(sheetId);
var histPricesSheet = activeSpreadsheet.getSheetByName(someSheetName);
....
var cell = histPricesSheet.getRange(range);
cell.setValues(someData);
}
function walkFolders(folderId){
// function that walk by folders and get ids of spradsheeds
and call func that I need in each spradsheed that I need
var fin_fold = DriveApp.getFolderById(folderId);
var folders = fin_fold.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
var files = folder.searchFiles('title contains "карточка"');
while (files.hasNext()){
var file = files.next();
updatePrices(file.getId()); // call my function to every spreadsheet
Logger.log("обновлено");
}
}
}
walkFolders("folder ID wich contain folders with files to change");
I have a standalone script that I need to run on Multiple Google Spreadsheets. I am able to assign a script to 1 spreadsheet using the following code:
function filter() {
var ss = SpreadsheetApp.openById('ID');
How to assign this to multiple spreadsheets?
There are more than a single way to do it.
1) You can manually get the id's of the various spreadsheets and hard code the id's as an array in the stand alone script.
2) You can move all the spreadsheets required to a single folder and automate opening the folder and opening the files in the particular folder. For this, say, the folder containing the required spreadsheets is "All spreadsheets", then try out the following code.
function myfunction()
{
var root = DriveApp.getFoldersByName("All spreadsheets");
while (root.hasNext())
{
var folder = root.next(); //If the folder is available, get files in the folder
var files = folder.getFiles();
while(files.hasNext()) //For each file,
{
var spreadsheet = SpreadsheetApp.open(files.next());
required_function(spreadsheet); //Call the required function
}
}
}
Hope it helps :)
I have replicated the google file picker in a web app.
My question is how would I tell the google picker to make a copy of the file when uploading it to google drive ?
Any help would be much appreciated.
I realised that google picker could not do what I needed so Google App Script to the rescue.
Firstly you need to create a new google spreadsheet
Under "Tools->Script Editor" add the following code
Thought trial an error I came up with the following.
function copyFiles() {
var dApp = DriveApp; // Get the drive app
var folderIter = dApp.getFoldersByName('parentFolder'); //Get folder my id
var folder = folderIter.next(); // Get the first folder
var fileIter = folder.getFiles(); // File iterator
var storageFolder = folder.getFoldersByName('childFolder').next(); // Get target folder
var subFolderIter = storageFolder.getFiles(); // sub folder File iterator
while(fileIter.hasNext()) {
var file = fileIter.next(); // Get the current File
var fileName = file.getName();
// Check if the current file exists in a coped folder
// If false make a copy else do noting
// This check prevents duplicate, Becasue makeCopy generates uniques ID everytime
if(!storageFolder.getFilesByName(fileName).hasNext()) {
//Logger.log('Files does NOT exists');
file.makeCopy(storageFolder);
} else {
//Logger.log('Files does exists');
}
}
}
Hope this helps anyone who might need.
Note: You will need to set up triggers for the script to run when required
Is there a way to make a google spreadsheet script run if the sheet is edited by IFTTT instead of by a person via the sheets browser?
Background: IFTTT put a value in a specific cell. My google spreadsheet script says if the cell is not empty; insert a new row. So my script is running if my sheet is edited by me manually and I put a value in that cell. However, if IFTTT adds a new entry to the spreadsheet nothing happens. I have tried both the onEdit and onChange triggers to no avail. Any suggestions?
The actual script I am using is below:
(function InsertRow() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Diagramm');
var values = sheet.getRange('B2').getValues();
if (values[0][0] != "")
sheet.insertRowBefore(2);
})();
So I followed the tutorial on Google Driver API Push Notifications and added following code to my script:
function listFiles() {
var optionalArgs = {
maxResults: 10
};
var response = Drive.Files.list(optionalArgs);
var files = response.items;
if (files && files.length > 0) {
Logger.log('Files:');
for (i = 0; i < files.length; i++) {
var file = files[i];
Logger.log('%s (%s)', file.title, file.id);
}
} else {
Logger.log('No files found.');
}
};
But what do I need to do now? Sorry I am not a good programmer...
You have to use Drive API push notifications to pick up these changes.
This example on using Apps Script with the Drive API is helpful to get started.
Spencer Easton has also provided a tutorial on get GMail push notifications that'll give you some hints on setting this up as you have to make some changes in the dev console.
I have a Google Sheets file with an attached Script. The script does a number of things, one is it makes a clone of it self using makeCopy. This portion works. Now I want to be able to keep the same cloned Google file name and same Google file ID and just update the content which includes a Spreadsheet and the associated Google script.
if (!fileFound){
var file = masterSSFile.makeCopy(reportFileName, RepFolder);
} else {
oldFile.setContent(masterSSFile.getBlob());
}
When I use makeCopy with the same file name it creates a second file with the same name but with a different file ID.
The else portion fails because .setContent argument seems to just accept text. The result is the word "Blob" in the oldFile, everything else is gone.
I have other scripts that update the contents of a existing spreadsheet by overriding the contents of the various sheets, but I also want the associated script to also be included in the updated file keeping the same file ID.
I found this....
Overwrite an Image File with Google Apps Script
and tried using
var masterSpreadsheetID = SpreadsheetApp.getActiveSpreadsheet().getId();
var masterSpreadsheetFile = DriveApp.getFileById(masterSpreadsheetID);
var oldFileID = oldFile.getId();
var oldFileName = oldFile.getName();
var newBlob = masterSpreadsheetFile.getBlob();
var file = {
title: oldFileName,
mimeType: 'application/vnd.google-apps.spreadsheet'
};
var f = Drive.Files.update(file, oldFileID, newBlob);
I get error: "We're sorry, a server error occurred. Please wait a bit and try again. " on this line: "Drive.Files.update(file, oldFileID, newBlob);"
After reading this:
https://github.com/google/google-api-nodejs-client/issues/495
it looks like Drive.Files.update(), does not support bound scripts.