Google App Script - save to Folder from another Person - google-apps-script

I would like to save Documents into a open folder in Google Drive from another person.
I tried the following code but it doesnt work if i´m not the owner of the code.
function saveToFolder() {
var folder = DriveApp.getFolderById("###");
//var destSpreasheet = SpreadsheetApp.open(DriveApp.getFolderById(SpreadsheetApp.getActiveSpreadsheet().getId()).moveTo(folder))
//var ss = SpreadsheetApp.getActiveSpreadsheet().
//get active Sheet
var sSpreadsheet = SpreadsheetApp.getActive;
var sheetname = sSpreadsheet().getActiveSheet().getName();
var sSheet = sSpreadsheet().getSheetByName(sheetname);
//var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sSpreadsheet().getId()).makeCopy("test",folder))
var destSpreadsheet = DriveApp.getFileById(sSpreadsheet().getId()).makeCopy(SpreadsheetApp.getActiveSpreadsheet().getName(), folder)
}
The goal is that a lot of people can safe there google sheets to the same folder in google drive by using a button.
Thanks a lot for your help

You have some typos in your code like getActive() and using sSpreadsheet() instead of sSpreadsheet (please refer to the documentation linked). Here is your "fixed code" that takes a Spreadsheet and creates a copy of it in a Drive folder (the code has self-explanatory comments):
function myFunction() {
// Get folder by ID
var folder = DriveApp.getFolderById("FOLDER ID");
// Get active Spreadsheet (if this script is not bounded use openById or openByUrl)
var sSpreadsheet = SpreadsheetApp.getActive();
// Make copy
DriveApp.getFileById(sSpreadsheet.getId()).makeCopy(SpreadsheetApp.getActive().getName(), folder)
}
Also bare in mind that in order to use DriveApp you must first enable the Drive API service.

Related

App Script for saving a sheet as PDF to a specific folder

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);
}
}

Google Apps Script, Google Sheets - Getting Spreadsheet ID and Sheet ID programmatically

I've been reading this without making any headway:
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app
Is there a way to programmatically get the Spreadsheet ID and Sheet ID of a particular Google SpreadSheet's sheet without manually typing or copying the URL?
1.) If the script is bound to the spreadsheet then you can just use getID and getSheetID as mentioned by TheMaster in the comments. See codes below:
function getIDs() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var spreadSheetID = ss.getId();
var sheetID = sheet.getSheetId();
console.log(spreadSheetID);
console.log(sheetID);
}
Result:
Take note that the first sheet always has an ID of 0 (gid=0) as the default value.
2.) Otherwise if what you are trying to do is get the Spreadsheet ID and Sheet ID from different spreadsheets it is not possible without copying the URL since there is no way it can identify which spreadsheet to refer to.
3.) Suggestion
What you can do if you need to get IDs of different files is to move them to a folder in your drive, then you can use the code below:
function listFilesInFolder(folderName) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1,1,sheet.getLastRow(),sheet.getLastColumn()).clearContent();
sheet.appendRow(["Name", "Spreadsheet ID", "Sheet ID"]);
//change the folder ID below to reflect your folder's ID (look in the URL when you're in your folder)
var folder = DriveApp.getFolderById("HIS_SHOULD_BE_YOUR_FOLDER_ID");
var contents = folder.getFiles();
var cnt = 0;
var file;
while (contents.hasNext()) {
var file = contents.next();
cnt++;
data = [
file.getName(),
file.getId(),
];
var spreadsheetSheets = SpreadsheetApp.openById(data[1]).getSheetByName("Sheet2").getSheetId();
data.push(spreadsheetSheets);
sheet.appendRow(data);
};
};
Folder:
Result:
Note: In my example code I've made use of Sheet2 to see that it is working as expected. Please set accordingly to the name of the sheet which you are trying to get the ID.
Let me know if this works for you or if you have other questions.
References:
Are Google Spreadsheets' first sheets always given an ID of 0?
Getting all files ID in drive folder

Open Google Spreadsheet via Google Apps Script after creating a copy

I am creating a copy of my google spreadsheet, and now want to close the current spreadsheet and open the one i created a copy. I know i can open it using the sheet id, but i dont know the sheet id yet because I want to achieve this using one macro!
current code -
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxx");
DriveApp.getFileById(sheet.getId()).makeCopy("Test File", destFolder);
//want to open this copied file
}
By modified your code a little bit, then you can obtain the copy sheet Id with newsheet.getId() function:
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxxxV6N-A4Z77f9OijHtAe2VhAU");
var newsheet = DriveApp.getFileById(sheet.getId()).makeCopy("Test File", destFolder);
Logger.log(newsheet.getId())
}

Copy one sheet to many others spreadsheets at the same folder

I have many spreadsheets inside a folder and today I have developed a new sheet (tab) inside one of these spreadsheets. I have a code to copy and paste one sheet to another spreadsheet, but for this there is another way without code.
My goal is:
a way to copy one sheet (order number 13) to all other spreadsheets at the same folder.
This is my code for copying one-to-one:
function sendspreadsheet(){
var source = SpreadsheetApp.getActiveSpreadsheet();
var aba = source.getSheets()[13];
var destination = SpreadsheetApp.openByUrl('https:sheetID');
aba.copyTo(destination);}
I have a great help from #Tanaike about similar issue at post: "Array for Google Sheet celarcontet" and I have studied this code and Google Class Sheet, but something is not working.
Code I'm trying to copy one-to-multiple spreadsheets at the same folder:
Function sendtomultiple(){
var source = SpreadsheetApp.getActiveSpreadsheet();
var aba = source.getSheets()[13];
var destination = DriveApp.getFolderById('1o6p-53Q1ntJAVIJt9w4k0UK___XXXXXX');
var sheetDestino = destination.getFilesByType(MimeType.GOOGLE_SHEETS);
while (sheetDestino.hasNext()) {
SpreadsheetApp.open(sheetDestino.next()).getSheets().forEach(aba => {
aba.copyTo(sheetDestino);
});
};
}
I guess this line into while instruction is the problem, I have tried other options, but all them return error message, kind this:
Exception: The parameters (DriveApp.FileIterator) don't match the method signature for SpreadsheetApp.Sheet.copyTo.
Hope someone can help to fix this.
Explanation / Issue:
Your goal is to copy a particular sheet (tab) into multiple spreadsheets within the same folder.
The issue has do to with the fact that sheetDestino.next() is a file object but copyTo(spreadsheet) accepts a spreadsheet object.
You can get the id of the file and then use openById(id) to get the spreadsheet object which you can use to copy the sheet to.
Bonus code:
I added an if condition to make sure the code does not try to copy the sheet to the origin spreadsheet file.
I added a code to rename the copied sheet to the destination sheet to the original name. The default would be copy of Sheet14...
Solution:
function sendtomultiple(){
const source = SpreadsheetApp.getActiveSpreadsheet();
const source_id = source.getId();
const aba = source.getSheets()[13];
const destination = DriveApp.getFolderById('1o6p-53Q1ntJAVIJt9w4k0UK___XXXXXX');
const sheetDestino = destination.getFilesByType(MimeType.GOOGLE_SHEETS);
while (sheetDestino.hasNext()) {
let target_file = sheetDestino.next();
let target_id = target_file.getId();
let target_ss = SpreadsheetApp.openById(target_id);
if(target_id!=source_id){
aba.copyTo(target_ss).setName(aba.getName());
}
};
}
Keep in mind that sheet getSheets()[13] is the 14th sheet in the spreadsheet file. If you want to get the 13th sheet you need to use getSheets()[12].

Google apps spreadsheet parents folder ID

I have a google spreadsheet that is keeping track of the files and their names that are inside the folder where the spreadsheet is.
I have looked and can not find how to get the location or ID of that sheet
In MS CMD script it would be "%~dp0".
How to do this in google apps spreadsheet script.
function FnctnMenuUpdateGetLocation() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSID=ss.get.getId();
var file = DocsList.getFileById(SSID);
Browser.msgBox(SSID);
Browser.msgBox(add);
}
https://docs.google.com/spreadsheet/ccc?key=0AmsQN3N9km70dGwtLTJXVVBzbmJhdmE5OFpkbTVxelE&usp=sharing
ya, I am working in it
this is a live sheet
From what I have how do you get the address of the spreadsheet in google drive
from the spread sheet I will be having it add folder/dir and file that I need
since google docs is not google drive how is the interface from Spreadsheet to the google drive
This works as of June 2018
This answer also assumes that you are only interested in the first folder in the list of parents.
var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
var spreadsheetFile = DriveApp.getFileById(spreadsheetId);
var folderId = spreadsheetFile.getParents().next().getId();
Both the Document Service and Drive Service can report which folder(s) a spreadsheet is in.
Here's your function, updated to show the name of a folder that contains the spreadsheet, using both services.
function FnctnMenuUpdateGetLocation() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSID=ss.getId();
var fileInDocs = DocsList.getFileById(SSID);
var folderInDocs = fileInDocs.getParents()[0].getName();
var fileInDrive = DriveApp.getFolderById(SSID);
var folderinDrive = fileInDrive.getParents().next().getName();
Browser.msgBox("Docs says: " + folderInDocs +
", Drive says: " + folderinDrive);
}
Document Service's .getParents() method returns an array of folder objects, while Drive's returns an iterator, so the details of how to retrieve a specific parent folder(s) once you've got all of them differs.
Note that a spreadsheet may be "contained" in multiple folders... in this case, we've assumed that the first folder in the list is the only one - that assumption may be invalid.