I wrote a code to create a new file this file I put a page of my spreadsheet after I move it to another folder, the problem is on the move it. I've tried to makecopy function and addtofolder but neither worked all returned an error.
this is my code:
function pasta(){
var fonte = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var root = DocsList.getRootFolder()
var base = DriveApp.getFoldersByName('Violino').next();
var pasta1 = base.createFolder('Subfolder2');
var destino = SpreadsheetApp.create("teste");
var destinoF = DocsList.getFileById(destino.getId());
fonte.copyTo(destino);
destinoF.makeCopy("x",pasta1);
destinoF.removeFromFolder(root);
//Browser.msgBox();
}
I corrected the problem by adding this line
var pasta2 = DocsList.getFolderById(pasta1.getId());
was a problem types, even both folder1 and folder2 varying types having the same name, they belong to different classes
My corrected and running code looked like this:
function pasta(){
var fonte = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var root = DocsList.getRootFolder()
var base = DriveApp.getFoldersByName('Violino').next();
var pasta1 = base.createFolder('Subfolder2');
var pasta2 = DocsList.getFolderById(pasta1.getId());
var destino = SpreadsheetApp.create("teste");
var destinoF = DocsList.getFileById(destino.getId());
fonte.copyTo(destino);
destinoF.addToFolder(pasta2);
destinoF.removeFromFolder(root);
}
Related
The script is triggered when a Google form is submitted and then auto-fills a Google doc.
It worked perfectly before I added var servicesPTY = e.values[117]; and replaced all the placeholders perfectly. But as soon as I add it then the executions indicator show completed but no documents are produced anymore. The document has placeholders that look like this: {{servicesPTY}} {{regNumberPTY}} {{tradingNamePTY}}
And the code looks like this:
function myFormSubmitPTY(e) {
var regNumberPTY = e.values[112];
var taxNumberPTY = e.values[111];
var tradingNamePTY = e.values[113];
var servicesPTY = e.values[117];
var file = DriveApp.getFileById("16OwyBIZAD2pwkuUXZnYSj-9WB6ObGGRXiEjDLa1tcjw");
var folder = DriveApp.getFolderById("1kogpJdxHLwuEhbVyh2oiIgTPH0SNac2m");
var copy = file.makeCopy(tradingNamePTY, folder);
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
if (type == "PTY (LTD)") {
body.replaceText("{{servicesPTY}}",servicesPTY);
body.replaceText("{{regNumberPTY}}", regNumberPTY);
body.replaceText("{{tradingNamePTY}}", tradingNamePTY);
doc.saveAndClose();
}
}
This works for me:
function testmyFormSubmit() {
var e={values:["one","two","three","four"]};
myFormSubmitPTY(e);
}
var type="PTY (LTD)";//global
function myFormSubmitPTY(e) {
var regNumberPTY = e.values[0];
var taxNumberPTY = e.values[1];
var tradingNamePTY = e.values[2];
var servicesPTY = e.values[3];
var file = DriveApp.getFileById("fileid");
var folder = DriveApp.getFolderById("folderid");
var copy = file.makeCopy(tradingNamePTY, folder);
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
if (type=="PTY (LTD)") {
body.replaceText("{{servicesPTY}}",servicesPTY);
body.replaceText("{{regNumberPTY}}", regNumberPTY);
body.replaceText("{{tradingNamePTY}}", tradingNamePTY);
doc.saveAndClose();
}
}
file name: three
pattern order:
{{servicesPTY}}
{{regNumberPTY}}
{{tradingNamePTY}}
output order:
four
one
three
I must have deselected the trigger for PTY. When I looked at the stackdriver logs I noticed PTY had none. Must have happened in the early hours of the morning. Thanks though. You guys put me on the right track
I am trying to get the name of the Parent Folder for a spreadsheet that is determined by url and then place the name in a specified cell. Debugging the script below tells me "TypeError: Cannot find function getParents in object Spreadsheet." I have tried every tweak I can think of and read multiple similar articles that may answer my question, but I am unable to understand them. (Forgive the newbie?) Can somebody tell me what I am doing wrong?
function getParent(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("VALUES");
var ll = s.getRange("B11");
var url = ll.getValue();
var driveFile = SpreadsheetApp.openByUrl(url);
var parentFolder = driveFile.getParents();
while (parentFolder.hasNext()) {
var folder = parentFolder.next();
var title = folder.getName();
}
s.getRange("B5").setValue(title);
}
It seems like this code has two small issues which we'll fix to get you up and running on this.
If possible, it is generally a better practice to work with document
IDs rather than URLs. Also, for a reason I'm not aware of, DriveApp
does not have a getFileByURL method. So I have changed your "url" variable for "fileId", and changed the contents of cell B11 to the Id of the file, which is part of the URL itself.
In order to return a Drive File, you should call getFileByID() not on your Spreadsheet itself, but on the DriveApp service.
I've tested the following code and I believe it does what you expect:
function getParent(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("VALUES");
var ll = s.getRange("B11");
var fileId = ll.getValue();
var driveFile = DriveApp.getFileById(fileId);
var parentFolder = driveFile.getParents();
while (parentFolder.hasNext()) {
var folder = parentFolder.next();
var title = folder.getName();
}
s.getRange("B5").setValue(title);
}
Try this:
function getParent(){
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName("VALUES");
var url=sh.getRange("B11").getValue();
var file=DriveApp.getFileById(url.split('/')[5]);//gets the id out of the url
var parentFolder=file.getParents();
while (parentFolder.hasNext()) {
var folder = parentFolder.next();
var title = folder.getName();
}
sh.getRange("B5").setValue(title);
}
I am trying to create a new Google Sheet in a created folder, but am having issues with the content Type.
I have searched for this error but haven't found anything. Most searches come up with a MimeType error, but I don't think that's the issue. Below is the code I'm using:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var newFolder = DriveApp.getFolderById("MyID").createFolder("New Folder");
newFolder.createFile("myFileName",ss,MimeType.GOOGLE_DOCS);
Whenever I run this in my spreadsheet, I get the following error:
Invalid argument: file.contentType (line 3, file "Code")
I have tried using "" for the content for createFile.
How about following sample? I cannot use MimeType of GOOGLE_SHEETS and GOOGLE_DOCS, too. I don't know why. So I propose use of SpreadsheetApp.create(). The flow is as follows.
Create spreadsheet using SpreadsheetApp.create().
Add destination folder information to the spreadsheet.
Remove original folder information from the spreadsheet.
By this, you can create a spreadsheet to a folder you want.
Script:
var sheetfile = "myFileName";
var destfolder = "MyID";
var sheet = SpreadsheetApp.create(sheetfile);
var file = DriveApp.getFileById(sheet.getId());
var destfolder = DriveApp.getFolderById(destfolder).addFile(file);
var docfile = file.getParents().next().removeFile(file);
Updated: November 25, 2020
At July 27, 2020, the method of moveTo(destination) was added. And, the methods of addFile and removeFile will be deprecated. Ref So, when the method of moveTo(destination) is used for above script, it becomes as follows.
var sheetfile = "myFileName";
var destfolder = "MyID";
var sheet = SpreadsheetApp.create(sheetfile);
var file = DriveApp.getFileById(sheet.getId());
var destfolder = DriveApp.getFolderById(destfolder);
var docfile = file.moveTo(destfolder);
When the method of Files: insert of Drive API is used, the following script can be also used. Ref
var sheetfile = "myFileName";
var destfolder = "MyID";
Drive.Files.insert({title: sheetfile, mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: destfolder}]});
this methode work with me
function myFunction() {
var sheetfile = "myfilename";
var destfolder = "MYFOLDER";
var sheet = SpreadsheetApp.create(sheetfile);
var file = DriveApp.getFileById(sheet.getId());
var destfolder = DriveApp.getFoldersByName(destfolder).next();
var docfile = file.moveTo(destfolder);
}
without next(); it not working for me
I faced the same issue and instead of trying to create a file on the fly, I thought of having a cleaner solution where by I am keeping one empty spreadsheet (Just create a spreadsheet in the drive and name as "Template") as a template to make copies out of.
var templateFileId = "asfashfkhsfkhfkhkashf"; //This is something you have to define as a constant
var destinationFolderId = "sdfashfkhaskf"
var newSpreadsheet = DriveApp.getFileById(templateFileId).makeCopy("NewSpreadsheet", destinationFolderId);
I'm trying to get the following code, what am I missing? Bottom line, I need to move or create the NEW spreadsheet in the folder. But it seems I need an ID for that, which I can seem to pull.
Thanks!
Rudy
var ss =SpreadsheetApp.create('Credit Card Issues with EZ-Pay')
Logger.log('CREATING FILE.');
//var folder = DocsList.getFolder("EZ-Pay Reports");
//var sheet = ss.getSheetByName('Sheet1');
var sheet = ss.getSheets()[0];
Logger.log(ss.getSheetId('Sheet1'));
var ssID = ss.getSheetId('Sheet1');
DocsList.getFileById(ssID).addToFolder(DocsList.getFolder("Credit Card Issues with EZ-Pay/EZ-Pay Reports"));
code is much simpler :
function createInFolder(){
var ss =SpreadsheetApp.create('Credit Card Issues with EZ-Pay')
DocsList.getFileById(ss.getId()).addToFolder(DocsList.getFolder("Credit Card Issues with EZ-Pay/EZ-Pay Reports"));
DocsList.getFileById(ss.getId()).removeFromFolder(DocsList.getRootFolder());
}
The DocsList service has been deprecated. Use the Drive Service instead:
var ss = SpreadsheetApp.create("Credit Card Issues with EZ-Pay");
var id = ss.getId();
var file = DriveApp.getFileById(id);
DriveApp.addFile(file); // adds to the root folder
You can also add the file to a folder other than the root:
var folder = DriveApp.getFolderbyId(id);
folder.addFile(file);
If you don't know the id of the folder:
var folderIter = DriveApp.getFoldersByName("My Folder");
while (folderIter.hasNext()) { // returns true if the collection has a next item
var folder = folderIter.next(); // throws an exception if there's no next item
}
folder.addFile(file);
I use the following code to create a new spreadsheet.
function addNewSpreadsheetWithFolderShift()
{
var source = SpreadsheetApp.getActive();
var SSID=source.getId();
var fileInDocs = DocsList.getFileById(SSID);
var folder=fileInDocs.getParents()[0];
var folderName = folder.getName();
var folderID = folder.getId();
var TimeZone=Session.getTimeZone();
var DTStamp= Utilities.formatDate(new Date(), TimeZone, 'yyyy:MM:dd HH:mm:ss zzz');
var User=Session.getEffectiveUser().getEmail();
var output_name=DTStamp+"LineChecks"
var ssOut = SpreadsheetApp.create(output_name); // Create a new spreadsheet, output_name.
ssOut.addToFolder(folder);
}
It winds up in my root directory. I would like it to wind up in the directory I started from. I tried using addToFolder but it seems that only files can be moved using it. Is there some way to move the newly created spreadsheet?
You can use DocsList service to move your file after its creation, try like this :
...
var ssOut = SpreadsheetApp.create(output_name); // Create a new spreadsheet, output_name.
var root = DocsList.getRootFolder();
var folder = DocsList.getFolderById('your folder Id');// or use getFolderByName if you want
var ssOutId = ssOut.getId();
var newFile = DocsList.getFileById(ssOutId); // begin the 'move to folder process'
newFile.addToFolder(folder);
newFile.removeFromFolder(root);// had to do it in 2 separate steps
}
Even though this question is 2 years old I thought I would post an updated solution since DocsList has been deprecated. I was trying to figure out the same issue and came across this answer using DriveApp.