I want to have my app script generate output to a SPECIFIC sheet, not the first sheet in the document.
i am looking to do the following:
Have 2 app scripts run on timers for a single google sheet document
App Script 1 will put data into sheet1, named "order_1"
App script 2 will put data into sheet2, named "orders_2"
my code currently puts the output into the active sheet, which is always the first sheet in the document
function import_Completed_orders_FromGmail() {
var threads = GmailApp.search('subject: "orders"');
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
// Is the attachment a CSV file
attachment.setContentTypeFromExtension();
if (attachment.getContentType() === "text/csv") {
var sheet = SpreadsheetApp.getActiveSheet();
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
// Remember to clear the content of the sheet before importing new data
sheet.clearContents().clearFormats();
sheet.getRange(1, 1, csvData.length,
csvData[0].length).setValues(csvData);
}
}
is there a way to define "ActiveSheet" by name? or to specify a specific sheet to put this output?
Use getSheetByName(name) method from Class Spreadsheet.
In order to get a Spreadsheet object you will have to use one of the SpreadsheetApp methods to open a spreadsheet like openById(id), open(file), openByUrl(url), getActiveSpreadheet().
You can get both sheet separately and use them in the code.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("order_1");
var sheet2 = ss.getSheetByName("order_2");
Related
I have a jot form that submits data to one dedicated google sheet (DGS). I work primarily with one google sheet doc that copies cell data from the DGS. I'm trying to implement a code that when new data is entered to duplicate data and send to the google sheet doc I use regularly.
*Note. If I try to alter the DGS, it throws an error and distorts the data sent.
function copydata()
{
var sourceSheet = SpreadsheetApp.openById("Google Sheet ID").getSheetByName("Tab Sheet Name");
var targetSheet = SpreadsheetApp.openById("Google Sheet ID").getSheetByName("Tab Sheet Name");
var rangeToCopyFrom = sourceSheet.getRange(sourceSheet.getLastRow(), 2, 1, 15);
var rangeToPasteTo = targetSheet.getRange(targetSheet.getLastRow(),1,1,15);
var rangeToCopyFrom = sourceSheet.getRange("A50:N50");
var rangeToPasteTo = targetSheet.getRange("A4:N4");
rangeToCopyFrom.copyTo(rangeToPasteTo, {contentsOnly:true});
}
This could be us to append data to another spreadsheet:
function onFormSubmit(e) {
const dss = SpreadsheetApp.openById("id");
const sh = dss.getSheetByName("Destination Sheet Name");
sh.appendRow(e.values);
}
It needs to go into DGS. I'd need to know more about the date to determine if it's not duplicate data
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'm wondering if when submitted, the data can be pasted to another separate file (and not tab). Do you think that there's a way?
You can access the file here : https://docs.google.com/spreadsheets/d/1FFlsfFyVyQobnbfAgQZA_r5IQeBhgOdK4cBpi5yo77U/edit?usp=sharing
Following code:
function myFunction(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var inputS = ss.getSheetByName("Input");
var outputS = ss.getSheetByName("Output");
var inputRng = inputS.getRange("A2:I2");
var values = inputRng.getValues();
inputRng.clearContent(); // clear input range
outputS.getRange(outputS.getLastRow()+1, 1, 1, values[0].length).setValues(values);
}
Explanation:
You can use openById(id) to access a different spreadsheet file by its id. As the documentation states:
For example, the spreadsheet ID in the URL
https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 is
"abc1234567".
and assuming that the target spreadsheet has a sheet named "Output" then the following code will work. Otherwise, change it to the name of the target sheet.
Solution:
function ScriptBS() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ts = SpreadsheetApp.openById(id); // put here the id of the target spreadsheet file
var inputS = ss.getSheetByName("Input");
var outputS = ts.getSheetByName("Output"); // get sheet Output of the target spreadsheet file
var inputRng = inputS.getRange("A2:I2");
var values = inputRng.getValues();
inputRng.clearContent(); // clear input range
outputS.getRange(outputS.getLastRow()+1, 1, 1, values[0].length).setValues(values);
}
I need to copy paste a range from template sheet file to current working sheet file. I understand from the below code, that is not possible.
Is it possible to achive this using Sheets API advanced service?
I actually need to copy paste cell widths, formatting, mergedcells etc from template file range to current file
I have tried with
function copyToMySheetFromTemplateFileRange()
{
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getActiveSheet();
var tss = SpreadsheetApp.openById(ID_OF_TEMPLATE_SPREADSHEET);
var tsheet = tss.getSheetByName("SHEET_A")
tsheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns())
var newSheet = ss.insertSheet(1);
newSheet.getRange(2, 1).activate();
tsheet.getDataRange().copyTo(newSheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
}
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.