I have created SSRS report and want to change the name of file with adding Date to it. I really appreciate your help.
Already tried https://reportsyouneed.com/ssrs-adding-date-to-exported-filenames/ solution and it doesn't work.
An easy alternative might be
An alternative approach to creating unique files for every delivery is to include a timestamp in the file name. To do this, add the #timestamp variable to the file name (for example, CompanySales#timestamp). With this approach, the file name is unique by definition, so it will never be overwritten.
See https://learn.microsoft.com/en-us/sql/reporting-services/subscriptions/file-share-delivery-in-reporting-services?view=sql-server-ver15
I got a perfect solution for this Issue.
This link works (very well) only if there is no parameter required before export.
Code in this link try to get the file name when you click on the file, means when download not ready and report looking for initial inputs.
But my file has date parameters in the beginning to input.
So I modify the code to wait till download is available and added to the end of "C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportManager\js\ReportingServices.js" file
function ModifyExportFileName(){
var rv=null;
var r= null;
var today =new Date();
var day= ("0" + today.getDate()).slice(-2);
var month = ("0"+ (today.getMonth() + 1)).slice(-2);
var year = ("0"+ today.getFullYear()).slice(-2);
var text= "DD";
try{
rv=this.$find("ctl31");
r=rv._getInternalViewer();
var url=r.ExportUrlBase;
var i = url.indexOf("FileName=");
}
catch(err)
{
//console.log(err);
setTimeout(ModifyExportFileName,2000);
return;
}
if(r==null)
{
setTimeout(ModifyExportFileName,2000);
return;
}
else
{
var url=r.ExportUrlBase;
var i = url.indexOf("FileName=");
var j = url.indexOf("&",i+1);
var oldFileName= url.substring(i+9,j);
var filename=text.concat(year,month,day)
r.ExportUrlBase= url.substring(0,i) + 'FileName=' + filename+ url.substring(j);
}
//console.log(filename);
setTimeout(ModifyExportFileName,2000);
}
ModifyExportFileName();
I need date as a filename so date is added as "filename". you can change it as required
Related
I'm trying to select multiple files depending on the mime type (JPG and Google Docs in this example) and move to specific folders for each type. So I tried the following:
function test() {
var srcfolderId = "abcdefghi";
var jpgFolderID=DriveApp.getFolderById(srcfolderId).createFolder("jpgfiles").getId();
var gdocFolderID=DriveApp.getFolderById(srcfolderId).createFolder("gdocfiles").getId();
var jpgfiles=DriveApp.getFolderById(srcfolderId).getFilesByType(MimeType.JPEG);
var gdocfiles=DriveApp.getFolderById(srcfolderId).getFilesByType(MimeType.GOOGLE_DOCS);
jpgfiles.moveTo(jpgFolderID);
gdocfiles.moveTo(gdocFolderID);
}
But this creates the error:
TypeError: jpgfiles.moveTo is not a function
What should I need to do to escape from this error? Should I use a different way to move multiple files to a specific folder?
Modification points:
The reason for your issue of TypeError: jpgfiles.moveTo is not a function is due to that the argument of moveTo is the folder object. In your script, the folder ID is used.
DriveApp.getFolderById(srcfolderId) can be declared as one variable.
getFilesByType returns FileIterator.
When these points are reflected in your script, it becomes as follows.
Modified script:
function test() {
var srcfolderId = "abcdefghi";
var folder = DriveApp.getFolderById(srcfolderId);
var jpgFolder = folder.createFolder("jpgfiles");
var gdocFolder = folder.createFolder("gdocfiles");
var jpgfiles = folder.getFilesByType(MimeType.JPEG);
while (jpgfiles.hasNext()) {
jpgfiles.next().moveTo(jpgFolder);
}
var gdocfiles = folder.getFilesByType(MimeType.GOOGLE_DOCS);
while (gdocfiles.hasNext()) {
gdocfiles.next().moveTo(gdocFolder);
}
}
References:
getFilesByType(mimeType)
moveTo(destination)
Try adding something like this:
while(jpgfiles.hasNext()) {
let file = jpgfiles.next();
file.mmoveTo()....
This is going to be a really stupid question - how do I open a file in the Filing Cabinet and read it in, line by line, using SuiteScript? Every example I can find online seems to start in the middle, taking for granted knowledge that I don't possess.
Is there a simple example somewhere online I've not found? All I need is for it to:
Open file (giving file name and folder)
Read the first line
Read the second line
....
Close the file.
In Suitescript 2.0 use the N/file module. The module can only be used in server side (not client) scripts. Reference Suite Answer Id: 43524 for N/file module and Suite Answer Id: 43520 for Script Types.
require(['N/file', 'N/record'], function(file, record) {
//use file name and folder and 'N/search' module to find the file id if necessary
// load file
var myFile = file.load({
id: '__' //enter the file internal id, absolute or relative file path
})
//get the # of lines
var arrLines = myFile.getContents().split(/\n|\n\r/);
// loop through each line, skipping the header
for (var i = 1; i < arrLines.length - 1; i++) {
//split the 1 line of data into an array. If the file was a csv file, each array position holds a value from the columns of the 1 line of data
var content = arrLines[i].split(',');
// get values from the columns of a CSV file
var column1 = content[0]; //first column
var column2 = content[1]; //second column
//can use the column data above to i.e. create new record and set default value, update existing records, write the data elsewhere
//to check each line for a given value
arrLines[i].includes('keyword'); //returns true or false
}
});
You can get the suitescript api documentation here
https://docs.oracle.com/cloud/latest/netsuitecs_gs/docs.htm
look at the file module
Let's keep it simple:
For SuiteScript 1.0:
var arrLines = nlapiLoadFile({fileinternalid}).getValue().split(/\n|\n\r/);
For SuiteScipt 2.0:
var arrLines = file.load({
id: {fileinternalid}
});
arrLines.getValue().split(/\n|\n\r/);
arrLines.description = 'My CSV File'
var fileId = arrLines.save();
...
// Add additional code
I have created a Google Form that logs Timestamps, a numerical value, and an image file from its respondents to a Google Sheet. The image files are saved to an "imageFolder", but when I get too many responses, my imageFolder and Google Sheet get too large. Ideally, I want my imageFolder and its Google Sheet to stay below 50 entries.
I want to move the 10 oldest images in imageFolder to waitFolder. I want to save an array of those oldest values from column "How many?" before the entry is deleted. Then I want any new entries to replace the oldest ones who's information I have already save to waitFolder and the howMany() array (myArray10).
I know I have to move 10 images from "imageFolder" to "waitFolder" using functions along the lines of:
var text1 = "Response Form";
var text2 = "imageFolder";
var text3 = "Copy of imageFolder";
var text4 = "Wait Folder"
function moveFiles(sourceFileId, targetFolderId) {
var myFolder = DriveApp;
var files = myFolder.getFileById(sourceFileId).getFiles()
while (files.hasNext()) {
var file = files.next());
var dest = myFolder.getFolderById(targetFolderId);
dest.addFile(file);
var pull = DriveApp.getFolderById(sourceFolderId);
pull.removeFile(file);
}
}
function getMyId(text) {
var Ids = [];
var myFolder = DriveApp;
var folderIter = myFolder.getFoldersByName(text);
var folder = folderIter.next();
var folderIter = folder.getFiles();
while(folderIter.hasNext()) {
var file = folderIter.next();
var fileId = file.getId();
Ids.push(fileId);
}
return Ids
}
function getMyId10(text) {
var Ids = [];
var myFolder = DriveApp;
var folderIter = myFolder.getFoldersByName(text);
var folder = folderIter.next();
var folderIter = folder.getFiles();
for (var i = 0; i < 10; i++) { //Take the first 10
while(folderIter.hasNext()) {
var file = folderIter.next();
var fileId = file.getId();
Ids.push(fileId);
}
}
return Ids
}
function main() {
var imageFolderId = getMyId(text2);
var imageFolderId10 = getMyId10(text2);
var waitFolderId = getMyId(text4);
var Copy_imageFolderId = getMyId(text3);
moveFiles(imageFolderId, Copy_imageFolderId); //make a copy of imageFolder
moveFiles(imageFolderId10, waitFolderId); //Move first 10, remove from original
}
How can I move images from imageFolder to Copy_imageFolder?
How can I move the 10 oldest images from imageFolder to waitFolder?
How can I remove the 10 oldest images from imageFolder?
How can I limit the number of rows in my spreadsheet using Google script?
How can I overwrite my oldest rows with new entries/rows?
edit1: I am getting unexpected tokens in every function, and I am unsure why? It seems to pop up in the while loop of my function getMyId().
edit2: I see now why I was getting unexpected tokens. It seems that I was being irresponsible with my loops. I have replaced my 'while's with 'for's to amend this mistake.
edit3: I removed some unnecessary snippets of code to make it easier to follow.
edit4: Here is what my Form Response looks like in my Spreadsheet. The images are saved to subfolder imageFolder. But I can't grab the 10 array elements I want from the spreadsheet using howMany(). I also can't seem to move any of the files anywhere. When I call on moveFiles(), I get an unexpected error as soon as it asks for my DriveApp. How do I make my moveFiles() move my images from source to target folders?
Perhaps this is what you were looking for:
function moveFiles(sourceFolderId, targetFolderId) {
var srcFolder=DriveApp.getFolderById(sourceFolderId);
var desFolder=DriveApp.getFolderById(targetFolderId);
var files=srcFolder.getFiles();
while(files.hasNext()) {
var file=files.next();
desFolder.addFile(file);
srcFolder.removeFile(file)
}
}
I am trying to build a script which will be sending emails based on the data in a spreadsheet in google sheets.
Column 1 - emailAddress
Column 2 - subject
Column 3 - body
Column 4 - signature
Column 5 - fileName (name of the file I'd like to attach, will be different in each case)
Column 6 - checkbox (new functionality in G Sheets, marked= TRUE, unmarked=FALSE).
The aim is to send email only to those which are not marked as sent. After the script is done checkbox should change to TRUE to avoid duplications.
I wrote the below script however there are two issues:
How to make a fileName variable value which will be taken to code from column 5?
How to force the program to change the status of the checkbox to TRUE and to omit those which are already TRUE?
The function:
function Email() {
var rng = SpreadsheetApp.getActiveSheet().getActiveRange()
var sheet = SpreadsheetApp.getActiveSheet();
var data = rng.getValues();
for (i in data)
{
var rowData = data[i];
var checkbox = data[5]
var file = DriveApp.getFilesByName(data[4])
var emailAddress = rowData[0];
var body = rowData[2];
var subject = rowData[1];
var signature = rowData[3];
var message = body + '\n\n'+ signature;
if(checkbox is != 'TRUE')
{
GmailApp.sendEmail(emailAddress,subject,message,
{
attachments: [file.next().getAs(MimeType.PDF)]
}
);
cell.setValue("TRUE");
}
}
}
How to make a fileName variable value which will be taken to code from column 5?
Create a variable var fileName. For uniqueness purposes just add a counter at the end 1,2, etc so it ends looking like fileName1, fileName2, etc
How to force the program to change the status of the checkbox to TRUE and to omit those which are already TRUE?
Use a double for loops because working with sheets is like working with 2D arrays. Inside that create an if statement:
if( getValue() is != 'TRUE'){
cell.setValue("TRUE")
}
Hopefully you got the idea from the pseudocode.
Trying to make a simple google apps script to get files by name and order them by date created. If there are more than 5 files by the same name, delete all but the 5 newest files.
function tryme(){
var files = DriveApp.getFilesByName('thisFile');
var created = files.getDateCreated();
for(i in created) {
if(created[i] > 4){
file.setTrashed(true);}
}
}
You're trying to use a File method on a File Iterator (returns from getFilesByName() method).
Here is a solution for your issue:
function tryme(){
// Get the file Iterator
var files = DriveApp.getFilesByName('New Text File');
var fileArray = [];
// Put file on array
while(files.hasNext()){
var file = files.next();
fileArray.push([file, file.getDateCreated()]);
}
//While you have more than 5 files
while(fileArray.length>5){
var older = ["",new Date()];
var olderIndex;
// Get the older file
for(var i in fileArray){
if(fileArray[i][1].getTime() < new Date(older[1]).getTime()){
olderIndex = i;
older = fileArray[i];
}
}
// Delete the older file
fileArray.splice(olderIndex,1);
older[0].setTrashed(true);
}
}
Edit: I made a mistake by using DriveApp.removeFile() instead of File.setTrashed(true)