I am simply trying to duplicate an open sheets file. I tried so many variation without success. The below is an example. Would value any assistance. Thank You.
function copyDocs() {
var file = DriveApp.getFilesByName('My Income Statement');
file.makeCopy();
}
Data retrieved using DriveApp.getFilesByName() is FileIterator. In this case, file is retrieved by using next().
There are 2 patterns for the sample script.
Sample script 1
Pattern 1 :
If the filename is only one in your Drive, you can use following sample script. This sample copies the file using the filename.
function copyDocs() {
var file = DriveApp.getFilesByName('My Income Statement').next();
file.makeCopy();
}
Pattern 2 :
If there are files with the same filename and you want to copy one of them, you can use following sample script. This sample copies the file using the fileID. The fileID can be retrieved as follows.
For document,
https://docs.google.com/document/d/### File ID ###/edit
For spreadsheet,
https://docs.google.com/spreadsheets/d/### File ID ###/edit
Sample script :
function copyDocs() {
var file = DriveApp.getFileById("### File ID ###");
file.makeCopy();
}
Sample script 2
This sample is for opening the copied file using a dialog on spreadsheet. Since the copied file is opened as new window, please permit to open popup window.
Copy and paste following script to container-bound script of spreadsheet.
Run dialog().
Push a copy button on the dialog box on spreadsheet.
By above flow, the file with fileId is copied and opened as new window.
function dialog() {
var data = '<input type="button" value="copy" onclick="google.script.run.withSuccessHandler(openfile).filecopy();"><script>function openfile(url) {window.open(url);}</script>';
var html = HtmlService.createHtmlOutput(data);
SpreadsheetApp.getUi().showModalDialog(html, 'Sample dialog');
}
function filecopy(){
var fileId = "### File ID ###";
return DriveApp.getFileById(fileId).makeCopy().getUrl();
}
Related
I'm rather new to Google Scripts. We are wanting to make all files uploaded to a single folder in Google Drive be automatically moved to other folders, based on part of their file name.
3 example files: APX PMT 05.02.2019, ALT PMT 05.03.2019, BEA PMT 05.04.2019
We want these files to be moved to their destination folders based on the first 3 letters of their file name. APX PMT 05.02.2019 gets moved to the APX folder, ALT PMT 05.03.2019 gets moved to the ALT folder, ect.
Do not have code samples as I'm extremely new to this. Move files automatically from one folder to another in Google Drive is a good start on me learning this, but still unsure how to make it move file based on only part of the file name.
Results: Wanting people to be able to upload files to a single destination, and the code auto moves them to their proper folders.
Test Code version 2.0 . Works as below if I remove the spaces between the 2 character sets (change BEA RFT to BEARFT or BEA_RFT) , as our workplace would like them sorted by the first 7 characters in the file name now. How can i make it work when there is a space in the characters?:
function moveFiles() {
var dfldrs=['BEA RFT', 'BEA ADJ', 'BEA PMT', 'BEA CHG'];//Seven letter prefixes
var ofObj={BEA RFT:'ID',BEA ADJ:'ID',BEA PMT:'ID',BEA CHG:'ID'};//distribution folder ids
var upldFldr=DriveApp.getFolderById('ID');
var files=upldFldr.getFiles();
while(files.hasNext()) {
var file=files.next();
var key=file.getName().slice(0,7);
var index=dfldrs.indexOf(key);
if(index>-1) {
Drive.Files.update({"parents": [{'id': ofObj[key]}]}, file.getId());
}
}
}
Moving Files
Please, Read these instructions before running script
You need to provide the three letter prefixes
You need to provide the distribution folder ids associated with each prefix
You need to provide the upload folder id
You need to run this program from your upload file script or provide an alternate trigger function as you desire.
You need to enable Advance Drive API version 2
The Code
function moveFiles() {
var dfldrs=['APX','ALT','BEA'];//Three letter prefixes
var ofObj={APX:'APX id',ALT:'ALT id',BEA:'BEA id'};//distribution folder ids
var upldFldr=DriveApp.getFolderById('folderid');
var files=upldFldr.getFiles();
while(files.hasNext()) {
var file=files.next();
var key=file.getName().slice(0,3);
var index=dfldrs.indexOf(key);
if(index>-1) {
Drive.Files.update({"parents": [{"id": ofObj[key]}]}, file.getId());
}
}
}
Drive API Reference
I want to search for a file, say with name having the date time stamp (DDMMYYYYhhmmss)(14122017143339). But in the server possibilities are there that the filename which I am expecting can be like either (14122017143337 OR 14122017143338 OR 14122017143339 OR 14122017143340) as there is a minute change in the seconds.
Now, I am trying to search for the file with only a portion of its name say like (DDMMYYYYhhmm)only uptil the minute. Meaning the file which i am expecting should contain the string (141220171433) in its name.
Can someone help on how can we achieve this Using Java?
Note - Am using Selenium for my coding purposes.
Below code in in Java and will find all files in a folder. you can search for required file name to match
File[] allFiles = new File("FOlder path").listFiles();
for (File f : allFiles)
{
if (f.isFile())
{
if(file.getName().contains("DDMMYYYYhhmm"))
{
System.out.println("true and file found");
// do something here
}
}
}
As in Excel multiple rows are present, having different set of data and I want to take screenshot for each and every step like this
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot1.png"));
driver.get("http://www.yahoo.com/");
File scrFile1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile1, new File("c:\\tmp\\screenshot2.png"));
How the same will be repeated for each Excel row.
As if I hardcode the value of "Screenshot1, ...2, etc." at the end I will get only 2 screenshots instead of 2 screenshots for each row record.
Create a FileName string instead of hardcoded file path, You can pass row number in screenshot file name for better identifications of screenshot file.
File ScreenShot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
StringBuilder FileName = new StringBuilder("C:\\tmp");
FileName.append(ScreenshotName);
FileName.append("_");
FileName.append(rownumber);
FileName.append(".jpeg");
FileUtils.copyFile(ScreenShot, new File(FileName.toString()));
The following code works fine when returning a file on a mac since it automatically appends the
file extension to the name of the file.
On windows however i have to type in the extension of the file as part of the file name in order for it to return with that extension....even though the extension is selected in the 'save type as' pulldown menu.
is there a way to automatically append the extension to the name when returning a file from the filechooser on windows?
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(fileExtension.toUpperCase()+" files(*."+fileExtension+")", "*."+fileExtension);
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
final File file = fileChooser.showSaveDialog(MyStage.this);
I ran into the same issue. My solution was to make a new File, and append the file extension as a string in the File constructor.
If you want users to be able to select and overwrite an existing file, make sure and add a check to make sure the initial save file does not contain the particular extension already before appending or else you will get something like "test.xls.xls".
FileChooser fc = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("XLS File (*.xls)", "*.xls");
fc.getExtensionFilters().add(extFilter);
File save = fc.showSaveDialog(stage);
save = new File(save.getAbsolutePath()+".xls");
FileOutputStream fileOut = new FileOutputStream(save);
i have a form that contains two file element with unique name. i want to reach each file element from Zend_File_Transfer_Adapter_Http(); how can i do that?
if i use
$apt = new Zend_File_Transfer_Adapter_Http();
.
.
$apt->receive();
two files have been uploaded. but i execute different queries for each file element.
for example
$smallPic = small pic name that i get from input smallPic
i execute
update products set smallPic = '$smallPic'
and for large pic
$largePic = large pic name that i get from input largePic
i execute
update products set largePic = '$largePic'
how can i reach each input file element with $apt ?
Here is some code I have used in the past to receive multiple files from a Zend_Form where many of the file upload fields were dynamic and not part of the form.
Since you know the names of the two file uploads, you can also say $apt->getFileInfo('my_file');
$apt = new Zend_File_Transfer_Adapter_Http();
$files = $apt->getFileInfo();
foreach($files as $file => $fileInfo) {
if ($apt->isUploaded($file)) {
if ($apt->isValid($file)) {
if ($apt->receive($file)) {
$info = $apt->getFileInfo($file);
$tmp = $info[$file]['tmp_name'];
$data = file_get_contents($tmp);
// here $tmp is the location of the uploaded file on the server
// var_dump($info); to see all the fields you can use
}
}
}
}
Hope that helps.