How can I rename sheet in a different spreadsheet? - google-apps-script

I have copied multiple sheets from one spreadsheet to another using the copyTo(spreadsheet) method. As the documentation suggests,
The copied sheet will be named "Copy of [original name]".
How can I rename that sheet, which is now effectively located in a different spreadsheet than the one hosting my script?

I know this can be done with a Spreadsheet when making the copy but I don't believe this is possible with just a copy of a sheet (with a single call).
I think you'll need to get it by the 'Copy of' name and use setName.

Related

Inserting multiple CSVs from to one Google sheet

I have a recurring process where I have a zip file with 4 CSVs that I want to add to a google sheet (new or existing). Is there a way to automate this?
It's just a one time import, not live data.
To do this manually, use File > Import four times and choose the Append to current sheet option each time.
To automate this with a Google Sheet formula, the data must be in .csv files rather than a .zip file, and those files must be reachable through the web without authentication.
If you can automate the unzip process and place the files in the cloud someplace, say a Dropbox folder, so that the file URLs are always the same, you can use importdata() like this to concatenate the files:
={
importdata("...url1.csv");
importdata("...url2.csv");
importdata("...url3.csv");
importdata("...url3.csv")
}
To automate this with Apps Script, use an installable trigger to run a function that uses UrlFetchApp, Utilities.gunzip and Utilities.parseCsv() to get the data, Array.concat() to merge the files, and Range.setValues() to write the results in the spreadsheet.
To reference a cell or range of cells in another worksheet in the same workbook, put the worksheet name followed by an exclamation mark (!) before the cell address.
In other words, in an Excel reference to another worksheet, you use the following format:
Reference to an individual cell:
Sheet_name!Cell_address
For example, to refer to cell A1 in Sheet2, you type Sheet2!A1.
Reference to a range of cells:
Sheet_name!First_cell:Last_cell
For example, to refer to cells A1:A10 in Sheet2, you type Sheet2!A1:A10.
After this you will write formulas ıf you add new excel only you can add formula SheetXX!A1:A10.

Using google script to copy a updated folder to another sheet, then deleting the original folder and rename the new one to the old name

I have written a google script that copy's a sheet to another workbook where the sheet exist, resulting in a folder with a name of 'Copy of ...". Let call that Sheet 'Original'
once done the workbook will have a folder called 'Original' and 'Copy of Original'
I use script to delete the 'Original' folder
I use script to rename the 'Copy of Original' to 'Original'
Problem is now the newly copied file which has additional folders can not reference the new 'Original' and gives an error.
Please advise as to the order this process should be done.
Thanks
Preston
I changed the process of CopyTo() to another spreadsheet and then deleting the original sheet and naming the 'Copy of' back to the original sheet name. I now CopyTo() and use the enums to update the original in the same sheet range.copyTo(orig,SpreadsheetApp.CopyPasteType.PASTE_FORMAT,false);
range.copyTo(orig,SpreadsheetApp.CopyPasteType.PASTE_FORMULA,false);
range.copyTo(orig,SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION,false);
range.copyTo(orig,SpreadsheetApp.CopyPasteType.PASTE_CONDITIONAL_FORMATTING,false);
range.copyTo(orig,SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS,false); I then delete the 'Copy of..' sheet and the references do not seem to be effected. Thanks for taking the time to help.

How to copy Google pivot table (values and format) to another spreadsheet using appscript?

I have a spreadsheet(say A) which contains a pivot table in one of the sheets.
Another sheet contains the source data for that pivot table.
I want to only copy the table from the sheet to another spreadsheet(say B) as I have to send the spreadhsheet B containing the single sheet in email.
I tried the following solutions suggested in the similar issue link below but they don't seem to solve my problem:
How to copy a row from one google spreadsheet to another google spreadsheet using google apps script?
Copy sheet from spreadsheet A to spreadsheet B. This failed because
the source data sheet was not in B.
Tried range.copyTo() but this failed too because it only works if
applied on the same spreadsheet.
Copied values i.e used getValues() and setValues(). This might work
but I also want the format to be copied.

Switch from one spreadsheet to another in the same tab in Google Sheets with a script

I've been trying to build a script which I can use to switch from a given sheet in one spreadsheet, to a given sheet in another spreadsheet, in the same browser tab, where both spreadsheets are on the same google drive and in the same folder.
The purpose for doing this is I have more data than I can fit on a single google sheets spreadsheet (More than 200 individual sheets are needed) and I'd like to be able to navigate between them as seamlessly as possible.
I could have sworn I had a simple script like this working at some point, but I'm not sure. The script does appear to run, but it there is no result, it doesn't actually open the other sheet.
I've tried using both .openByID and .openByUrl, and both appear to give me the same result.
I set up two new spreadsheets, A and B, each with their own script, A pointing to B, and B pointing to A. The destination sheet within the spreadsheet has been named MainA and MainB.
//This is your "Go Home" Button
//URL of A: https://docs.google.com/spreadsheets /d/1LzsxZ3cCcOltELM_0T4VwipqYv5BbBDj9ugAZcciNHQ/edit#gid=0
//URL of B: https://docs.google.com/spreadsheets/d/1wrjr9VSsHb63RKz87JQbe20mCo1CteKHtsAiUGrb6O0/edit#gid=0
//ID of A: 1LzsxZ3cCcOltELM_0T4VwipqYv5BbBDj9ugAZcciNHQ
//ID of B: 1wrjr9VSsHb63RKz87JQbe20mCo1CteKHtsAiUGrb6O0
function goto_a() {
var ss = SpreadsheetApp.openById("1LzsxZ3cCcOltELM_0T4VwipqYv5BbBDj9ugAZcciNHQ")
ss.setActiveSheet(ss.getSheetByName("MainA"));
}
Obv the script for A is similar to this one, but with differently named function, destination sheet, and spreadsheet ID
In both cases, I'm using a button (drawing image) with the relevant script attached. If need be, I can share these spreadsheets so that the script can be tested.
Any advice would be great, thanks in advance.
As written in the official documentation ,
//Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
You can alternatively provide a link using Browser.msgBox() for the user to click.
Related Question

How do I copy a script from one spreadsheet to another spreadsheet?

I need to copy an existing script, which I did not write, into my existing spreadsheet. The script is called "SaveBack", and can be found here: https://docs.google.com/a/levelgroup.com/spreadsheet/ccc?key=0Agcb8bUVVOOodHhoV3BrRGZ6UEdSYnVLSEk3bllxRnc#gid=1.
My existing spreadsheet is too complex (it has many other spreadsheets that link to it) to copy all of its sheets into the spreadsheet that contains the script. I have already copied the SaveBack editor sheet template sheet to my main spreadsheet, but I can't figure out how to copy the SaveBack script that goes along with it into my spreadsheet.
Can anyone help? Thanks!
Open the save back script, select the script text, copy it, go to your sheet, create a new script (blank template), paste the script you copied into your new script, name is SaveBack (assuming it's a project), check the triggers on the original script and make sure your triggers match, and you should be good to go. You will have to change any sheet, document, or other string ID's to match your files' Id's, but that isn't too bad.
I do this sort of thing all of the time when I'm migrating things back and forth between my work and personal account. You could also create a copy of the other person's spreadsheet that you linked above, and it will move the script over with it. Then you can go to the script and get the project key to use it as library in your own scripts. Since you're using your copy as the library, you don't have to worry about someone else changing the script and breaking your functionality.