Google script DocsList service query string not working - google-apps-script

I'm making a little script to automatically organize some autogenerated spreadsheets. The goal is to archive the spreadsheets in a directory based on their name (all of them start with the same name but end in a pattern I'm using to organize them). The problem I have is with the function:
lstFile = DocsList.find('type:spreadsheet title:"PROG_GRAL_CENTRE"');
The function doesn't have the query options specified in the docs, but I'm using it on another script and is working fine! I've also tried putting only:
lstFile = DocsList.find('PROG_GRAL_CENTRE');
which should find 200 documents, but none is found! Actually, if I type PROG_GRAL_CENTRE into the search box of my google Drive, all the documents are found, so I don't know what's wrong with my search filter.
Any thoughts?

The method find(query) looks for a string in file content, not only on file name... so even it it worked normally (meaning without the issue you mentioned) I'm not sure it would be a good solution for your use case since there would be a risk that some filename could be found inside another doc with another name (as a reference for example...).
Why don't you try getting all filenames in an array and search into this array instead ?
This can be done very easily and would bring an elegant solution to your problem wouldn't it ?
I'm working on this very same type of script right now ;-) here is the part that gets my files (just an example if ever you are interested):
var doclist=DocsList.getRootFolder().getFilesByType("document",0,2000);
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
if(doclist[nn].getName().match("IMPRESSION_")=="IMPRESSION_"){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
}
after that I sort the array the way I want and show the result in a list UI.

Related

How to reference an external spreadsheet with Google Apps Scripts

Update: It worked for 5 minutes then stopped working again. Not exactly sure why it's only working part of the time?
This sounds like it's supposed to be simple but I'm only finding information on "getActive" and my function works if I'm using all internal sheets but I tried using openById and nothing happened and openByUrl returns an error. The following is a simplified example of what I'm trying to do:
var store_data_sheet = SpreadsheetApp.openByID('ID here..').getSheetByName('Data');
store_data_sheet.getRange('A1').setValue('testing')
store_data_sheet.insertRows(1);
I'm sure I'm just using the wrong keywords or maybe this isn't even allowed but multiple users will be using their own copy of a specific sheet that runs this function and I'd just like to take certain cell's data and copy it to one external sheet (which is why it also inserts a row so it can push down entries)
Maybe I'm using the wrong "ID"? I assumed the number inside the URL was the ID. If it's something more specific, please let me know.
Like I said, this works flawlessly if I'm referencing an internal sheet. I'd like to be able to do the same exact thing with an external spreadsheet if possible. Thank you!
I found the issue. This function also renames the sheet file so if there is anything in the cell that is used to rename the file, it won't run the storing functions. I either need to run these before the renaming or take out the renaming aspect. ---moving these before the renaming part fixed the problem.

Which query string for DriveApp.searchFiles() mimics the Drive search "is:unorganized"?

I would like to recreate in Google App Script the search "is:unorganized", which would be entered in the Google Drive search bar. My goal is to find orphaned files in a manner other than finding the parents of all files and selecting those which are 'null'.
According to the documentation the queries are written according to these rules.
However, it's not clear to me how to test for "unorganized". I've tried:
var orphans = DriveApp.searchFiles(q)
with the following qs
"unorganized=true"
"'unorganized' in parents"
For the first, I get the error "Invalid argument: q" because the query is invalid, and in the 2nd I get an error saying the ID doesn't exist because there is no folder called "unorganized".
Does anyone know the q that corresponds to "is:unorganized"?
Thanks.
My goal is to find orphaned files in a manner other than finding the parents of all files and selecting those which are 'null'.
There's no way currently in DriveApp.searchFiles(query) to compose a query that reproduces the is:unorganized Drive UI search. That's because the Drive API (DriveAPP uses V2) search fields and operators don't give you the breath of functionality required.
There is however a search that will improve upon a scan of all files, and that is using a search query as follows:
var potentialOrphans = DriveApp.searchFiles('not "root" in parents');
This gives you all files except those that are children of My Drive. This is a smaller starting list that you could potentially then iterate through to find those files that have no parents.

Scan an area of a web page's source code for changes while reporting it?

this is one heck of a confusing question to ask so here it goes. Firstly, I'm not asking you to write me any code I just need help going in the right direction for what I'm trying to achieve here. Basically the task is this, I want to scan a select area of a web page's source code for changes and if something does change, I want to report it somewhere (like a console or something). However, I do not want just a notification of change, I also want what the change is/was. I've been looking into things like jsoup but I am still struggling to even find out what this is called.
Any pointers would be insanely appreciated. Thanks, Optimistic.
Here are some steps assuming this is from a node.js project:
Get the URL for the specific script file you're looking for a change in.
Using the request() module, fetch that URL.
Break the data up into lines (probably using .split()).
Find the specific line you are looking for either by counting line numbers of by searching for some representative text in that line.
Using some sort of search in that line (perhaps a regex), find the current value of the exact item in that line you are looking for.
Save the current value.
Then, at some future time, repeat this whole process and compare what you find to the previous value.
If this is being done from a browser instead of node.js, then use an Ajax call to retrieve the file. If the file is on another domain from your web page and that domain does not permit cross-origin requests, then you cannot solve this problem in an automated fashion from a browser in your own web page.
Here is how I would do it with Jsoup:
Document doc = Jsoup.connect(url).get();
String scriptCssQuery = "script"; // Tune this CSS query to find THE script you need.
Element script = doc.select(scriptCssQuery).first();
if (script != null) {
String scriptLines = script.html();
// Store the changing line somewhere and compare it to its previous value...
}

Checking if a file has been edited

I want to know if there is a way to check if a file has been edited. I looked for methods that can make it for me on Google Apps library, but I have found nothing about it. I don't know if I searched wrong.
Basically, I need to take a file, take a measurable data (something like size) of this file and store on a variable. Then, I need to take that measurable data again, store on another variable, and compare if there was a change. I need a boolean return.
Anyone?
You could do the following (pseudo with links to documentation):
Get the file you want to check using the DocList Class.
Get the ID of that File once you have it using File.getID()
Get the last edit timestamp using File.getLastUpdated()
Store this value in a Spreadsheet, or maybe Script or User Properties.
When you want to check to see if the File was updated, simply File.getFileById()
Repeat step 3.
Then compare the two last-edited timestamps with an operator like !=, or do more complex comparisons on the Dates if you want.
Depending on the result of step 7, return true or false.
Google's documentation is great, for all their services. You just need to read it a bit to understand what kind of power you have through scripting. Hopefully my pseudo-method helps in solving your problem!
Look at the file update date: https://developers.google.com/apps-script/reference/drive/file#getLastUpdated() and for storing data look up the storing data section in the apps script help page.
You could also use the GAT General Audit Tool http://goo.gl/hzZ2yf... which reports when files were edited , viewed and much more.

Call functions from another "class" / file

Ok, I feel embarrassed that I wasn't able to figure this out on my own, but after a few wasted hours, I figured it would be easier to simply ask over here:
I have a bunch of .gs-files in my Google Apps Script project. Now, I want to call another file's function from a method (something like AnotherClass.awesomeFunction(), which throws a ReferenceError though). Is this possible in Google Apps Script? If so, how?
Files aren't classes. You can call any functions in any file from any other file. Think of your files as if they were just added together before running. If you want class-like scoping you can use the Libraries feature.
The Above replies are correct above file being appended, make sure the order of the files in the file explorer on the app script project page is correct
The Function definition should be in the first file and the function call in the latter.
You change the option of the each file by clicking the 3 dots next to file name and selecting Move file up or Move file down
The following syntax allows you to call any function from within your Google Apps Script project, regardless of whether the function is defined in the same file that is calling it:
myFunction();
The following code is unnecessary and will throw errors:
google.script.run.myFunction();
It can do.
and Corey is right, files is not class.
I'd just like to add that order of files is not important as experienced by me so far. I'm working on a project where all calls are at the start to get a clear tree and all definitions of functions are at the end. Sometimes they're even mixed without any order within files too. So, I guess, it can call function from anywhere regardless of order within file or within project files. It's working in my case though.