I am new to google app script. I am stuck in a situation. I have some ranges(created in spreadsheet) and I want to get the name of sheet associated to that ranges. The following function is on docs and I want to copy data by giving some ranges from sheet.
function selectTable(sheetURL,ranges,rangevalues )
{
var workbook = SpreadsheetApp.openByUrl(sheetURL);
var currentSheetId=workbook.getId();
// get the Spreadsheet by sheet id
var totalSheets = SpreadsheetApp.openById(currentSheetId);
// select the sheet
var sourcesheet = source.getSheetByName(rangevalues);//here i ma stuck I have ranges only and I want sheetname
var abc=sourcesheet.getNamedRanges();
Logger.log("source sheet name-:"+sourcesheet);
// get the values on selectd range
var srcData = sourcesheet.getRange(ranges).getValues();
Logger.log("getting the values-:"+srcData);
}
I am referring this answer
Copy a range of SpreadSheet to a Doc.
To get the name of a sheet from a range object use:
range.getSheet().getName()
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
I have an app script code that Creates a new workbook when new data is entered in Google Sheet. The code is below.
function myFunction() {
// The code below creates a new spreadsheet "New Sheet Name" and logs the URL for it
var ssNew = SpreadsheetApp.create();
Logger.log(ssNew.getUrl());
}
I want the new spreadsheet name to be the last data entered in Column A and also the URL for the new workbook to be pasted in Column J of the same row as the last data in Column A
From I have an app script code that Creates a new workbook when new data is entered in Google Sheet., I thought that you might be running the script with the installable OnEdit trigger. But from your showing script, I thought that you might have wanted to directly run with the script editor. If my understanding is correct, how about the following modification?
Modified script:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var title = sheet.getRange("A" + lastRow).getValue();
var ssNew = SpreadsheetApp.create(title);
sheet.getRange("J" + lastRow).setValue(ssNew.getUrl());
}
When this script is run, the title is retrieved from the column "A" of the last row. And, the URL of created Spreadsheet is put in the column "J" of the same row.
Reference:
getLastRow()
Creating a new Spreadsheet
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const nss = SpreadsheetApp.create(sh.getRange(sh.getLastRow(), 1).getDisplayValue())
sh.getRange(sh.getLastRow(), 10).setValue(nss.getUrl());
}
I am trying to import the User Report in a Google Admin account into Google Sheets. It is straightforward to export to a Google Sheet, but I would like to run a Google script that will pull in the User Report into an existing Google Sheet.
Try this.
function cloneGoogleSheet(existingSheet, userReportSheet){
//get source google sheet document
var source = SpreadsheetApp.openById(userReportSheet);
//get the source sheet on document
var sourceSheet = source.getSheetByName('NameOfSheet');
//get data range
var dataRange = sourceSheet.getDataRange();
// get A1 notation for the range identification
var A1Range = dataRange.getA1Notation();
//get data within the specified range
var data = dataRange.getValues();
//specify the existing spreadsheet you want to pull data to
var target = SpreadsheetApp.openById(existingSheet);
//target sheet on your existing document
var targetSheet = target.getSheetByName('NameOfTargetSheet');
//if there is already data on the target sheet, you may want to remove it
targetSheet.clear({contentsOnly: true});
//now you set the target range of the values you get from your user report
targetSheet.getRange(A1Range).setValues(data);
};
I have a customer sheet and a invoice sheet
When I go to the invoice sheet D12 and I type the invoice ID that's located in the customer sheet, then I get all the data from some vlookups.
But what I want is this:
I click on a cell in the customer sheet where the invoice number is located in that row. I activate the test1 function and the invoice number should be changed in the invoice sheet D12.
But when I run this script I get this error:
TypeError: Cannot find function setActiveSheet in object Sheet
Does anyone know how to change the invoice D12 cell?
function test1() {
var ss = SpreadsheetApp.getActiveSheet();
var row = ss.getActiveCell().getRow();
var rowSource = ss.getRange(row, 8);
ss.setActiveSheet(spreadsheet.getSheetByName('Invoice'), true);
ss.getRange('D12').activate();
ss.getCurrentCell().setValue(rowSource);
}
The code examples on https://developers.google.com/apps-script and other places use ss to hold a Class Spreadsheet object rather than a Class Sheet object.
Try replacing
var ss = SpreadsheetApp.getActiveSheet();
by
var ss = SpreadsheetApp.getActiveSpreadsheet();
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.