I'm developing a Google Script to import an HTML file into a new Spreadsheet.
To do so, my script works like that :
initialize an upload form
save the new file into Google Drive, in a public folder.
create a new spreadsheet
set a formula into the first cell : =IMPORTHTML, to import the html file, via the googledrive public URL (FYI, it only works with a public folder)
Here is my issue : I'd like to unlink the new spreadsheet from the html file, in order to be able to remove the HTML file, for security reason. To do so, I tried to :
copy the values and paste them with contentsOnly=true option, to a new sheet => ERROR
add a "Utilities.sleep" before that to be sure the HTML is well imported (just for paranoia, it doesn't change anything...)
It works only if I copy and paste without contentsOnly=true.
Here is my script : https://script.google.com/d/1NH3S3-TT-Yq43gqN4gkVwyTfhm9ZLz6aDVEB_w6gvvjFd3Xwdn0b0-Cg/edit?usp=sharing
Try this script removing the {contentsOnly:true} on line 58, it should work.
If you leave it, it generates this error : "An error occurred on the server(...)"
Could you help me ?
many thanks
I've found another way to do my copy :
I replaced
sourceAllData.copyTo(sheet2.getRange('A1'), {contentsOnly: true});
by :
sheet2.getRange(sourceAllData.getA1Notation()).setValues(sourceAllData.getValues());
Related
I'm trying to create a copy of a document and populate it with data from a spreadsheet. I can successfully create the copy but the script can't seem to access it to edit its contents. Here's the code:
var file = DriveApp.getFileById('1mWFhZpZVJpYJleg9n5qq3tr7EHwUIlbM');
var copy = file.makeCopy('SKPembimbing_' + nim, folder).getId();
var doc = DocumentApp.openById(copy);
var body = doc.getBody();
It always gives me an error at the 3rd line when it tries to open the document for editing.
(Exception: Document is inaccessible. Please try again later.)
I saw another thread from last year with the same exact question but without a solution. I've been working all night trying to get this darn thing to work. Does it have to do with my scope? I'm running it on a trigger on edit.
I found the solution:
The template document I was copying was a .docx file, NOT a Google Doc. The copied file was thus also a .docx file so was inaccessible from the DocumentApp. All I had to do was convert it to a Google Document.
I'm trying to modify this script found here:
https://github.com/ahochsteger/gmail2gdrive
I need the script to check if the file already exists on Google Drive and if not, create it. Currently, the script just creates the file into Google Drive, without checking to see if an existing file with the same name already exists or not.
I'm not a programmer, I know nothing about Google App Script (although I have managed to set it up and got it running) and I know nothing about JavaScript. I'm just wondering if someone could either point me in the right direction or help me code this one feature that I need?
From what I understand (I could be wrong), the attachment is created based on this line in the code:
var file = folder.createFile(attachment);
So then I tried add this before the createFile:
var file = folder.removeFile(attachment);
My logic here is that if the file exists in the folder, then remove it first before creating it (therefore avoiding duplicate files). But that didn't work.
From the script of GitHub in your question, it is found that attachment is blob. So how about using this filename? I think that there are several solutions for your situation. So please think of this as one of them. The flow of sample script is as follows.
Retrieve the filename of blob.
Retrieve FileIterator using getFilesByName().
If the FileIterator has values, it means that the file with the same filename has already been existing.
If the FileIterator has no values, it means that the file with the same filename is not existing.
The sample script is as follows.
Sample script 1:
If you want to create new file only when the file of same filename is not existing, you can use the following script.
var fileName = attachment.getName();
var f = folder.getFilesByName(fileName);
var file = f.hasNext() ? f.next() : folder.createFile(attachment);
Sample script 2:
If you want to do something when the file of same filename is existing, you can use the following script.
var fileName = attachment.getName();
var f = folder.getFilesByName(fileName);
var file;
if (f.hasNext()) {
// If the file has already been existing, you can do something here.
} else {
// If the file is not existing, you can do something here.
file = folder.createFile(attachment);
}
Note:
From your question, the file is searched from the folder. If you want to search the file from all files, please tell me.
References:
getName()
getFilesByName(name)
FileIterator
If this was not what you want, please tell me. I would like to modify it.
Im using the script editor in a Google spreadsheet, and I want to create a folder inside a root folder , already created. I've tried to follow the official notice of the API DriveApp, but without success.
Here is my code :
function createSubFolder(rootFolder,name){
DriveApp.getFoldersByName(rootFolder);
DriveApp.createFolder(name);
}
I've tried also ( following this old post working with the old version of DriveApp : i.e DocsList) :
function createSubFolder(rootFolder,name){
DriveApp.getFoldersByName(rootFolder).createFolder(name);
}
It seems to be very easy but I don't see where am I wrong.
Does anyone know the way to do it?
Thanks
You need to assign values to a variable.
function createSubFolder(rootFolder,name) {
var AllFoldersWithThisName = DriveApp.getFoldersByName(rootFolder);
var theFirstFolderWithThisName = AllFoldersWithThisName.next();
theFirstFolderWithThisName.createFolder(name);
};
In Google Drive, you could have 1,000 folders all with the same name. Same thing with file names.
The getFoldersByName() method returns:
FolderIterator — a collection of all folders in the user's Drive that have the given name
You need to get one of the folders out of the collection of folders. That is done with the next() method.
I am running C#.NET, and Aspose version 7.3.
When I export my sheet, it exports completely fine. No problems. Nuttin but love.
However, when I click 'save as' in the browser options (as opposed to 'open'), and open that resultant file, there is an empty spreadsheet titled "Object."
Please see the screen shot below.
http://imgur.com/yjHy2
Is there any way that I can suppress this? It's not showing in my worksheet collection at run-time. I am also having trouble googling it, as "object" is a pretty common word when it comes to Aspose Cells.NET development.
Your assistance is appreciated. Thanks.
Probably, you might be creating Object worksheet accidentally or may be Object worksheet already exist in your template file.
Anyway, please try the following sample code and see what results you get. It should work fine without any problem.
If it works fine, you can then modify your code according to this code.
For saving in Xls format, please use XlsSaveOptions
and
For saving in Xlsx format, please use OoxmlSaveOptions
Sample Code in C#
//Create a new workbook
Workbook workbook = new Workbook();
//Get the first worksheet in the workbook
Worksheet sheet = workbook.Worksheets[0];
//Rest of your code goes here
string yourFileFormat = "XLS";
//Save file and send to client browser using selected format
if (yourFileFormat == "XLS")
{
workbook.Save(HttpContext.Current.Response, "output.xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));
}
else
{
workbook.Save(HttpContext.Current.Response, "output.xlsx", ContentDisposition.Attachment, new OoxmlSaveOptions(SaveFormat.Xlsx));
}
HttpContext.Current.Response.End();
How would this script be modified? I would like to work this tutorial but I believe it was written before drive replaced docs.
Section 1: Embedding folders in a File Cabinet
Linking Google Docs Folders with Sites File Cabinet
Open the Script Editor in your Site.
Click More Actions > Manage Site.
In the left-hand sidebar, click Apps Scripts.
Click Launch Editor.
Copy and paste the following code into the editor.
function showFolderInSite() {
var files = DocsList.getFolderById("PASTE_FOLDER_ID_HERE").getFiles();
var page = SitesApp.getPageByUrl("PASTE_PAGE_URL_HERE");
for(i in files){
page.addWebAttachment(files[i].getName(), '', files[i].getUrl());
}
}
Open your folder in Google Docs and copy the folder ID from the URL that is in the browser window. The folder ID is everything after the folder.#. portion of the URL.
For example, given the following example url
https://docs.google.com/?tab=mo&authuser=0#folders/folder.0.0B3xR7WfXrj7aOGY4Y2M3OWItZjAyYy00ZTM5LTg3MTgtNjNkYWZhZTZmYmU3
the folder ID is this section: 0B3xR7WfXrj7aOGY4Y2M3OWItZjAyYy00ZTM5LTg3MTgtNjNkYWZhZTZmYmU3. Note that it is everything after the folder.0.. The following graphic also shows the folder ID:
There are some minor changes in the way folders url are constructed but the idea is always the same : the ID is everything thats is different between two items of the same type (2 spreadsheets, 2 folders ...that's just how they build up the urls)
In this example below, the ID is just eveything after folders/
ie the line of code to get the files in this folder would be like this :
var files = DocsList.getFolderById("0B3qSFd3iikE3TkN2THNDUnAwX2s").getFiles();
Hoping this will help you to finalize your tuto.
Note that some url are more complex in that some parameters are added behind the ID string (it is actually the case most of the time ;-)