I'm currently creating files using drive.makeCopy(Objects[i][1]); where Objects is the name of the various files I want to make from a template.
The script works fine in making these files from a template however after each one has been made I would like to open the new file, hide some sheets in it and to set its permissions. The addresses for the permissions are held in Objects[i][3] but I cannot work out how to open these new files, then make the changes accordingly and then close the file.
I'm assuming by drive.MakeCopy() you are referring to File.MakeCopy(). This is easy, the MakeCopy function returns the File object it has created. You can then set permission this File object using functions such as addEditor(), addViewer(), setSharing(), etc.
So if Objects[i][3] contained the email address of someone who should be able to edit the new file, then you could do:
var new_file = drive.makeCopy(Objects[i][1]);
new_file.addEditor(Objects[i][3]);
You don't really need to "Close" the file after working with it, Apps Script takes care of this automatically.
See:
https://developers.google.com/apps-script/reference/drive/file#addEditor(String)
https://developers.google.com/apps-script/reference/drive/file#setSharing(Access,Permission)
https://developers.google.com/apps-script/reference/drive/file
Related
I have been writing a script thats supposed to take information from several .odt files and insert that into several different spreadsheet cells and im stuck with the question of how to get that text into a variable so i can do some regex stuff to get the actual information i want. Problem is this:
let text = DocumentApp.openById(idString).getBody();
when i execute that, i get this error
Error
Exception: Invalid argument
I've read here that one of the issues might be that DriveApp ids are not the same as DocumentApp ids, in that case, ive tried getting the proper id first, but in that case, it still doesnt seem to work.
let text = DriveApp.getFileById(idString).getBlob;
now, that technically works, however, i still dont know how to retrieve the body/text/contents (really, however you wanna call it) from the file.
ive tried getDataAsString, but that just gets me well, everything but what i care about.
Hopefully someone can help here
Unfortunately, odt file cannot be directly opened as the readable data. So, in this case, in order to retrieve the text from odt data, I would like to propose the following flow.
Convert odt data to Google Document using Drive API.
Open the created Google Document using the Document service (DocumentApp).
Retrieve the text from the Document.
When this flow is reflected in a sample script of Google Apps Script, it becomes as follows.
Sample script:
Before you use this script, please enable Drive API at Advanced Google services.
function myFunction() {
const fileId = "###"; // Please set the file ID of odt file.
const id = Drive.Files.copy({title: "temp", mimeType: MimeType.GOOGLE_DOCS}, fileId).id;
const doc = DocumentApp.openById(id);
const text = doc.getBody().getText();
console.log(text);
}
When this script is run, the above flow is run. By this, you can retrieve the text data from odt file.
The converted document is created as a temporal file in the root folder. You can remove it with DriveApp.getFileById(id).setTrashed(true) after the script was finished.
Reference:
Files: copy
I am going to deploy this page on an FTP
And I need to find out how I can detect the html file currently being viewed using JavaScript.
If I open the html file, it works just fine with this:
var fileName = location.href.substring(location.href.lastIndexOf("/") +1);
But, if I open it via my localhost adress, it has a null value. So I'm guessing I have to use some other method to extract the current html file name. Or is there a better approach to this?
Note: I am not going to use JQuery or anything like that.
EDIT:
I can get the filename if it isn't my index file.. If it's the index file I get nothing using the above code. Most likely since all I have in my adress bar is the localhost adress of the live-server?
The web deals in URLs, not file names.
Sometimes a URL will include something that looks like a file name, and sometimes that even maps on to a real file name on the server's hard disk.
When you type http://example.com/ then it might map that onto a file called index.html. Or maybe on to index.php. Or maybe it won't touch any file but will just use logic built into the web server application to determine what to respond with.
There's no way to know in the general case.
If your specific case, you know that the path / maps onto index.html, so you can write an explicit mapping in your JavaScript code.
Does anyone have any suggestions for how to remove a file from shared with me? I need to do this without actually revoking access, as the people are added via a contact group, so only removing 1 person will not work.
Things to note:
I know using this can find the file:
var files = DriveApp.searchFiles('sharedWithMe');
//then I set file = what I'm looking for.
However, this will not remove the file from shared with me:
DriveApp.removeFile(file);
I've also tried brute forcing by patching the metadata, except it was to no avail.
In addition, it is wrong to assume that shared with me is a folder, as when listing parent folders the file returns nothing.
Only DriveApp cannot completely remove files. So it is necessary to use Drive API. "removeFile()" is used for changing parent folder information. It cannot remove files. I prepared 2 samples for removing files. If you are owner of the file, you can remove it.
Move file to trash box and empty trash :
var file = DriveApp.getFilesByName("filename").next();
file.setTrashed(true)
Drive.Files.emptyTrash();
Remove directly file :
Drive.Files.remove("fileID");
It was confirmed that above script can remove file even if other users are using.
For using above script, it is necessary to enable Drive API at Advanced Google Services and Drive API at https://console.developers.google.com/ Developers Console Project.
Files without parent folder :
If you had used "removeFile()", files without the parent folder may be existing in your drive. You can confirm them following script. This can retrieve file name and file ID of files without the parent folder.
var result = [];
var files = DriveApp.getFiles();
while (files.hasNext()) {
file = files.next();
if (!file.getParents().hasNext()) {
result.push([file, file.getId()]);
}
}
Logger.log(result);
I'm working on an HTML/javascript app intended to be run locally.
When dealing with img tags, it is possible to set the src attribute to a file name with a relative path and thereby quickly and easily load an image from the app's directory. I would like to use a similar method to retrieve a text file from the app's directory.
I have used TideSDK, but it is less lightweight. And I am aware of HTTP requests, but if I remember correctly only Firefox has taken kindly to my use of this for local file access (although accessing local images with src does not appear to be an issue). I am also aware of the FileReader object; however, my interface requires that I load a file based on the file name and not based on a file-browser selection as with <input type="file">.
Is there some way of accomplishing this type of file access, or am I stuck with the methods mentioned above?
The browser will not permit you to access files like that but you can make javascript files instead of text files like this:
text1.js:
document.write('This is the text I want to show in here.'); //this is the content of the javascript file
Now call it anywhere you like:
<script type="text/javascript" src="text1.js"></script>
There are too many security issues (restrictions) within browsers making many local web-apps impossible to implement so my solution to a similar problem was to move out of browsers and into node-webkit which combines Chromium + Node.js + your scripts, into an executable with full disk I/O.
http://nwjs.io/
[edit] I'm sorry I thought you wanted to do this with TideSDK, I'll let my answer in case you want to give another try to TideSDK [/edit]
I'm not sure if it's what you're looking for but I will try to explain my case.
I've an application which allow the user to save the state of his progress. To do this, I allow him to select a folder, enter a filename and write this file. When the user open the app, he can open the saved file, and get back his progress. So I assume this enhancement is similar of what you are looking for.
In my case, I use the native File Select to allow the user to select a specific save (I'm using CoffeeScript) :
Ti.UI.currentWindow.openFileChooserDialog(_fileSelected, {
title: 'Select a file'
path: Ti.Filesystem.getDocumentsDirectory().nativePath()
multiple: false
})
(related doc http://tidesdk.multipart.net/docs/user-dev/generated/#!/api/Ti.UI.UserWindow-method-openFileChooserDialog)
When this step is done I will open the selected file :
if !filePath?
fileToLoad = Ti.Filesystem.getFile(scope.fileSelected.nativePath())
else
fileToLoad = Ti.Filesystem.getFile(filePath)
data = Ti.JSON.parse(fileToLoad.read())
(related doc http://tidesdk.multipart.net/docs/user-dev/generated/#!/api/Ti.Filesystem)
Please note that those snippets are copy/paste from my project and they will not work without the rest of my code but I think it's enough to illustrate you how I manage to open a file, and read his content.
In this case I'm using Ti.JSON.parse because there is only javascript object in these files but in your case you can just get the content. The openFileChooserDialog isn't mandatory, if you already know the file name, or if you get it from another way you can use Ti.Filesystem in your own way.
How can I check to see if a file is already open by another user in javascript? As it is right now, the program I'm trying to fix will open/edit a file then fail on trying to save if the file is already in use.
Also, is there an easy way to add a lock on the file so another process knows it's in use?
Edit: the program is a .hta using Active X Objects.
i guess i should have been more specific, here's some code about how it is opening/editing/saving the files.
var FileSystem = new ActiveXObject( "Scripting.FileSystemObject" );
var xmlDoc = new ActiveXObject( "Msxml2.DOMDocument.3.0" );
var fFile = FileSystem.GetFile( strPath );
xmlDoc.load( fFile.Path );
// some method's to edit documentElement in xmlDoc...
xmlDoc.save( fFile.Path );
Are you sure it's just JavaScript and not a combo of maybe an ActiveX or flash component? Is the file on the client or server? If server, this question makes more sense to me (ie. using some AJAX solution).
I'm not too familiar with ActiveX, but maybe when you open a file you could create a temporary file like file.ext.lock (and delete it when you save the file), so when another user tries to open the same file and sees the .lock file exists, you know it's being used.
You would probably need a server side locking feature. The javascript would call the server's 'save' script, which would return either a 'successful' status, or 'file locked'.
The simplest lock method that most programs use is creating another file with the same name but an extension such as '.lock'. A process checks if the file exists when opening the original, if so the file is in use and can only be opened as read only. If not, the lock file is created and the original can be edited.
will open/edit a file then fail on trying to save.
Javascript cannot open files or save them.
That may be your problem.
It could "edit" them - you can use JS to manipulate or edit an HTML page. [Even running a whole Rich Text Editor.]
But you then have to pass the page back to some other script to actually save those changes.
This is actually not true if you have Aptana or similar server side Javascript, or if it is being used [mozdev] to pass data to SQLite which can save its own data. If this is your case you should specify, as it is hardly typical Javascript usage.