I am currently creating a script file bounded to a spreadsheet, where this spreadsheet is stored in a folder. Now, I need the name of the folder in which this spreadsheet exists. So how do I achieve this?
Solution:
The following script will get the name of the parent folder of the bound spreadsheet and set it to cell A1. Adjust the script to your needs.
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const ss_id = ss.getId();
const file = DriveApp.getFileById(ss_id);
const parent_folders = file.getParents()
const folder_name = parent_folders.next().getName(); //desired result
sh.getRange('A1').setValue(folder_name);
}
folder_name is the name of the parent folder of the existing spreadsheet.
Related
I am looking for an app script that lets me save a sheet as PDF from my Google Workbook to a specific folder in Google Drive based on a condition.
In my Google Drive I have a Google Sheet Workbook file and an employee folder.
The employee folder Id is 1Jy2Yr7u0qrgy90Gvtb6p8V27baS7T_eP
My google workbook has a sheet called 'Sheet2' whose Id is 1DmDBmEijUkolVp7aVgGNK4cTRu5y4uLdzbCrDYO5XRA.
This sheet has a cell C3 that has an employee name. The value in this cell needs to be checked, i.e. if the employee name matches then the script should convert the sheet to a PDF format and place the PDF in employee folder.
I am not a programmer or a coder but doing my best to get this working. I went through many scripts that I found here in stackoverflow and in Youtube and came up with this. I am not sure if it is correct. Need some expert help. Hoping someone can help.
function checkSheet() {
var sheetName = "1DmDBmEijUkolVp7aVgGNK4cTRu5y4uLdzbCrDYO5XRA";
var folderID = "1Jy2Yr7u0qrgy90Gvtb6p8V27baS7T_eP";
var sourceSpreadsheet = SpreadsheetApp.getActive();
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
var folder = DriveApp.getFolderById(folderID);
var checkForNews = spreadsheetApp.getActiveSpreadsheet().getSheetByName("Tracking").getRange("c6").getValue();
if (checkForNews='Yes') {
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder));
var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var newFile = folder.createFile(theBlob);
}
}
Here are the resource that I found,
Using Google Apps Script to save a single sheet from a spreadsheet as pdf in a specific folder
How to check for cell value?
How can I modify this to save a spreadsheet into a new folder
As mentioned above I came up with that script which I think is not right. Please help.
Try it this way:
function checkSheet() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Tracking");
var fldr = DriveApp.getFolderById("folder id");
if(sh.getRange("C6").getValue() == "Yes") {
var theBlob = ss.getBlob().getAs('application/pdf').setName("mypdf");
fldr.createFile(theBlob);
}
}
I have spreadsheet with multiple sheets inside. I'd like to make a macro which would copy two specific sheets into another spreadsheet and name it using format "MYNAME_YYYY_MM_DD" where YYYY_MM_DD is replaced with the date on which macro was ran.
So far i have this:
function X() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('INPUT'), true);
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('OUTPUT'), true);
};
Copy two spreadsheets into a folder
function lfunko() {
const ss0 = SpreadsheetApp.openById("id0");
const ss1 = SpreadsheetApp.openById("id1");
const file0 = DriveApp.getFileById(ss0.getId());
const file1 = DriveApp.getFileById(ss1.getId());
const folder = DriveApp.getFolderById("folderid");
file0.makeCopy(`myname${Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"yyyy_MM_dd")}`,folder);
file0.makeCopy(`myname${Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"yyyy_MM_dd")}`,folder);
}
trying to figure out how to give a new sheet that is copied into a folder a specific name each time from a button script. The specific name would come from a specific cell each time.
Here's the current code being used and the New Sheet is what I'm trying to have named from cell B3 on a specific spreadsheet each time. No idea how to do this, it just names it NEW SHEET each time and then errors out when I try to do B3.
function cloneGoogleSheet() {
var destFolder = DriveApp.getFolderById("Folder Name");
DriveApp.getFileById("Folder Name").makeCopy('NEW SHEET', destFolder);
SpreadsheetApp.getActive().getSheetByName('NEW SHEET').setName('Specific Sheet!B3')
}
Any help is appreciated!
function cloneGoogleSheet() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const name = sh.getRange('A1').getDisplayValue();
const destFolder = DriveApp.getFolderById("Folder Id");
DriveApp.getFileById("file id").makeCopy(name, destFolder);
}
Get used to using file id's and not names.
I am trying to find a way of creating a script that will copy a GoogleSheet, which is the Master file, into a new location with a new name. The name of the file would be located in Cell A1 with the folder location in Cell A2. With Excel it was relatively easy, but I just can't seem to get anything to work with GoogleSheets.
Assuming this is the Master file:
Replace cell A2 with the folder id of the destination folder, choose the name of the copy in cell A1 and change Sheet1 to your specific case.
Copy and execute this function from the script editor:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet1");
const new_name = sh.getRange('A1').getValue();
const dest_folder_id = sh.getRange('A2').getValue();
const dest_folder = DriveApp.getFolderById(dest_folder_id);
DriveApp.getFileById(ss.getId()).makeCopy(new_name, dest_folder);
}
References:
makeCopy(name, destination)
getRange()
Class Spreadsheet
I am trying to create a Google Sheet template where a user can enter a value (Project URN) in a cell and run a script to save the template as a sheet, renamed with the Project URN.
I have created a test Google Sheet here https://docs.google.com/spreadsheets/d/1IPpQyYHl_TtNa1lGpmu4dlzwTFjOotV2OcnxFT84iUk/edit?usp=sharing
This sheet has the following script, but I cant get the renaming function to correctly work.
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("0B7RrbZuJGLlha3NvM3ZqcTY3MDA");
DriveApp.getFileById(sheet.getId())
.(makeCopy(values = SpreadsheetApp.getActiveSheet()
.getRange(2, 2).getValues()), destFolder);
} //END function saveAsSpreadsheet
I did also find the following article but wasn't able to turn it to my use. However, the secret must be here somewhere! Rename worksheet to cell value keeping format
Kitten
You almost have it. Please try this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get spreadsheet
var id = ss.getId(); // Get spreadsheet ID
var sstocopy = DriveApp.getFileById(id); //Get spreadsheet with DriveApp
var sheet = ss.getActiveSheet(); // Grab the sheet in order to find the name of the new spreadsheet to be created
var sheet_name = sheet.getRange("A1").getValue(); // Get the value, in this case: Project Urn
var folder = DriveApp.getFolderById("XXXX"); // Get the folder where the sheet needs to be placed.
sstocopy.makeCopy(sheet_name,folder); // Make the copy of the sheet in that folder.
}
Let me know if you have any questions.