Copy Spreadsheet file (only visible sheets) - javascript

Is it possible to copy only the visible sheets of a Spreadsheet File with .copy()?
destination = ss.copy(destName);
This is the copy of the file, right now I'm making the copy and iterating the copy sheets to delete hidden sheets, but I think it would be better if I could directly make the copy without hidden sheets (not deleting sheets in the original file obviously).
Any ideas?

Try this:
function copyvis() {
const ss = SpreadsheetApp.getActive();
const dss = SpreadsheetApp.openById(dssid);
ss.getSheets().filter(sh => !sh.isSheetHidden()).forEach(sh => sh.copyTo(dss));
}

Related

Google apps script to duplicate script in a different folder giving type error when I try to access new sheet id ()

I am trying to run a google apps script that copies the current script to another folder but for some reason getId() after the copy function duplicateactivesheet is not allowing me to access the new sheets id.
TypeError: newSheet.getId is not a function
copySheetToFolder # Code.gs:29
I get a Type error after running:
function copySheetToFolder(folderId) {
// Get the sheet to be copied
//var sheet = SpreadsheetApp.getActiveSheet();
sheet = SpreadsheetApp.getActiveSpreadsheet();
// Make a copy of the sheet
var newSheet = sheet.duplicateActiveSheet();
// Get the ID of the new sheet
var newSheetId = newSheet.getId();
// Get the folder where the sheet will be copied
var folder = DriveApp.getFolderById(folderId);
// Add the sheet to the folder
folder.addFile(DriveApp.getFileById(newSheetId));
}
From your provided official document, duplicateActiveSheet() is the method of Class Spreadsheet. But, in your script, sheet of var sheet = SpreadsheetApp.getActiveSheet() is Class Sheet. I think that this is the reason for your current issue of TypeError: SpreadsheetApp.getActiveSheet(...).duplicateActiveSheet is not a function.
But, from a google apps script that copies the current script to another folder, I thought that you might have wanted to copy the active spreadsheet to the specific folder. If my understanding is correct, how about the following modification?
Modified script:
function copySheetToFolder(folderId) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
DriveApp.getFileById(ss.getId()).makeCopy(DriveApp.getFolderById(folderId));
}
By this modification, the active Spreadsheet is copied to the specific folder of folderId.
If you want to give a new title of Spreadsheet, please use DriveApp.getFileById(ss.getId()).makeCopy("new name", DriveApp.getFolderById(folderId)).
Note:
If you want to use duplicateActiveSheet(), please use the following modification.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var newSheet = ss.duplicateActiveSheet();
References:
duplicateActiveSheet()
getFileById(id)
makeCopy(destination)
Added:
Now, I noticed that you updated your question after I posted an answer. About your new issue of TypeError: newSheet.getId is not a function, I think that the reason of your new issue is due to that you are using the sheet ID as the spreadsheet ID. If you want to move the Spreadsheet to the specific folder after you copied the active sheet, please modify as follows.
Modified script:
function copySheetToFolder(folderId) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.duplicateActiveSheet();
var ssId = sheet.getId();
var folder = DriveApp.getFolderById(folderId);
DriveApp.getFileById(ssId).moveTo(folder);
}
From your showing script, in this modification, the active sheet is copied and the active Spreasheet is moved to the specific folder.
In the current stage, addFile of Class Folder has been deprecated. Please be careful about this. Ref In this modification, moveTo is used. Ref

Running Macro on Multiple Google Sheets Workbooks

I have a large directory of workbooks that I need to change the parameters of a named range. I have recorded a Macro that can do this and I'm sure there's a quick way to apply?
I have a Tab in a directory workbook called 'Planner Fixes' & I have a list of Sheet ID's to send the amendment too.
The Macro is as below:
function ElementsEdit() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('API BootstrapStatic'), true);
spreadsheet.getRange('A2:BK514').activate();
spreadsheet.setNamedRange('Elements', spreadsheet.getRange('A2:BK1000'));
spreadsheet.getActiveSheet().hideSheet();
};
I know that the line var spreadsheet = SpreadsheetApp.getActive(); needs to change to look for the spreadsheet ID and 'open' that?
I'm very new to scripts and have generally only used ones I have found online but I can't seem to find anything to do this at all.
If you have multiple workbooks, and if you know the id of each of them, try
var spreadsheet = SpreadsheetApp.openById(YOUR_SPREADSHEET_ID);
for each
You can loop through a list of id
const ids = [
'1T7ixa-JGLgh8oxxxxxxxxxo4UzbivflUlwL9zLGVkgg',
'16uEeVNEBzBbGVTgKa98yyyyyyyyyyyy6mU-yKIZ0fKM',
'1d3ZHizzzzzzzzzzzzzzzzzzzLkmM4bFHyDn8RFtAlkk'
]
function myFunction() {
ids.forEach((id, i) => {
var spreadsheet = SpreadsheetApp.openById(id)
// your script here
})
}

Open String CSV data in Google Sheets

So I currently have some CSV formatted data as a string in my JavaScript code as part of a chrome extension. What I need is a button which when clicked will open a new tab with that CSV data loaded in as a Google Sheets document. Basically a "Open in Google Sheets" button. The opened Google sheet document will preferably not be stored in the signed in user's drive (or any drive), but just open a new tab with the CSV file opened.
$("#opnWithSheetsBtn").click((e)=>{
myCSV = $("#userInput").value()
// Code which will open CSV data in new tab using Google Sheets
})
TLDR: I want to create a "Open with Google Sheets" button in a front-end application.
Solution
Taking into account the information that you shared my assumption is the following: you have your CSV data converted into a Javascript object and stringified. Correct me if I am wrong.
If my assumption is right, then, on the HTML button click you would need to run the function provided below. To be able to run this in Apps Scrip you must go to Resources->Advanced Google Services and activate the Sheets API service (as we will be using its method batchUpdate).
This Sheet API function will grant us with a better efficency than compared to using vanilla Apps Script.
function insertCSVinNewSheet(data){
// Use create to create a new Spreadsheet and get its first sheet.
var sheetId = SpreadsheetApp.create('newSpreadSheet').getSheets()[0].getSheetId();
var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
// Create the resource to update your sheet
var resource = {
requests: [
{
pasteData: {
data: data,
coordinate: { sheetId: sheetId },
delimiter: ","
}
}
]
};
// Use the Sheet API to make a batch update in the new sheet inserting all our data
Sheets.Spreadsheets.batchUpdate(resource, spreadsheetId);
}
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

After selecting a range on a Google Sheet, how do I paste the selection into a Google Doc?

I'm trying to create a macro on a Google Sheet that will select a range from the sheet and then paste the selected contents into to a Google Doc. So far I have the code to select the range:
function CopyToDoc() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A4:B13').activate();
}
Once I select the range, how do I paste it into a google doc?
Well to achieve what you are saying I would do this.
function sheetToDoc(){
var app = SpreadsheetApp;
var as = app.getActiveSpreadsheet();
var sh = as.getSheets()[0];
var temp = sh.getRange("A1:A20").getValues();
var doc = DocumentApp.openById("DocID").getBody().appendTable(temp);
}
Use AppendTable() to add the information to your document in a table and appendParagraph() if you want to add the information to your document as plain text. Make test for you to know how the information will be added to your document.
I hope this helps. Greetings

How to move a sheet from one Workbook to another using apps script?

I would like to move a sheet ("HeaderSheet") from one workbook ("HeaderSpreadsheet") to another workbook ("ReportSpreadsheet").
Is it possible??
I tried the following without success:
var HeaderSpreadsheet = SpreadsheetApp.openByUrl('Url_1')
var ReportSpreadsheet = SpreadsheetApp.openByUrl('Url_2')
HeaderSheet = HeaderSpreadsheet.getSheetByName("Header")
ReportSpreadsheet.insertSheet('Data', 1, {template: HeaderSheet})
Alternatively, could I copy the content from the Header workbook to the Report one?
Thank you very much for your help
I found my question was already solved.
This may be useful:
Google Apps Script, copy one spreadsheet to another spreadsheet with formatting

Categories