How to get files labels

217 views
Skip to first unread message

Artem Volkhonskyi

unread,
Jul 15, 2023, 3:40:43 AM7/15/23
to Google Apps Script Community
I try to get list of all files on Shared Drive called "ERP" with the filename, author, file labels. But every time with different approach I receive an error. I was try to solve this with Google Bard, but without any success. 

I try this script:
function getFiles() { var spreadsheet = SpreadsheetApp.openById('YOUR_SPREADSHEET_ID'); var sheet = spreadsheet.getSheets()[0]; var files = Drive.Files.list().filter(function(file) { return file.labels.length > 0; }).map(function(file) { return [file.name, file.lastModifiedDate, file.author, file.labels]; }); sheet.getRange('A1').setValues(files); }
receive:  TypeError: Drive.Files.list(...).filter is not a function
Drive API enabled and I receive an errors in Google Console for drive.files.list function
In ScriptApp Project settings add an ID number of Google Cloud Project.



cbmserv...@gmail.com

unread,
Jul 15, 2023, 3:19:51 PM7/15/23
to google-apps-sc...@googlegroups.com

This script is out of date. Those properties were deprecated a while back. Labels is no longer in use.

 

Try this:

 

function getFiles() {

  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

  var sheet = spreadsheet.getSheets()[0];

  var files = Drive.Files.list();

  var listItem = files.items.map(function(file) {

    return [file.title, file.modifiedDate, file.ownerNames, file.createdDate];

  });

  sheet.getRange(1,1,listItem.length,listItem[0].length).setValues(listItem);

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/4334364e-089a-4edd-aaad-1e5f15f3855en%40googlegroups.com.

Artem Volkhonskyi

unread,
Jul 16, 2023, 3:30:08 AM7/16/23
to Google Apps Script Community
I'll try your version, but received "Attempted to execute getFiles, but could not save." But when I check the availability of this function with this code everything is ok:

function getFiles() {
var files = Drive.Files.list();
try {
files.save();
} catch (e) {
// Handle the error.
}
}

P.S: Sad to hear about labels. Because I use labels, I try to make it more useful. Thank you!

cbmserv...@gmail.com

unread,
Jul 16, 2023, 4:14:20 AM7/16/23
to google-apps-sc...@googlegroups.com

The script is expected to be inside a spreadsheet for it to work. It is not a standalone script.

 

Try adding it to a spreadsheet and then run it.

Artem Volkhonskyi

unread,
Jul 16, 2023, 4:39:58 AM7/16/23
to Google Apps Script Community
I'm not sure I understand your right. I added the script in a standard way: Spreadsheet>Extensions>AppScript.

CBMServices Web

unread,
Jul 16, 2023, 1:31:15 PM7/16/23
to google-apps-sc...@googlegroups.com
Ok, yes, that is what I was referring to. You can run some scripts standalone and outside of a container like a spreadsheet, form, etcc). But thr small function is setup to be used as container based inside a spreadsheet.

I just added it to a spreadsheet myself and ran it and it worked fine. It does require the Google Drive API to be added. Did you add the Drive API as a service?


Artem Volkhonskyi

unread,
Jul 25, 2023, 5:24:39 AM7/25/23
to Google Apps Script Community
Yes, I add Drive API as a service

cbmserv...@gmail.com

unread,
Jul 25, 2023, 11:58:49 PM7/25/23
to google-apps-sc...@googlegroups.com

Then the script should work for you.

 

The only reason you would get this error:

 

TypeError: Drive.Files.list(...).filter is not a function

 

Is if you do not have access to the folder in question.

Reply all
Reply to author
Forward
0 new messages