I'm trying this script to create data validation but it's always doesn't work
function test_validation()
{
var Spread = SpreadsheetApp.getActiveSpreadsheet();
var Sheet = Spread.getSheetByName('Sheet1');
var validate1 = Sheet.getRange("A1").getDataValidation();
Logger.log(validate1.getCriteria());
Logger.log(validate1.getCriteriaValues());
var option = new Array();
option[0]="true";
option[1]="1";
option[2]="2";
option[3]="3";
var criteria="ITEM_IN_LIST";
var helpText="Test Help Text"
var Combobox = Sheet.getRange("A2").getDataValidation();
//The line below display error
//"Cannot find method (class)setCriteria(string,String[]). "
Combobox.setCriteria(criteria,option);
Combobox.setHelpText(helpText);
var validate2 = Sheet.getRange("A2").setDataValidation(Combobox);
Logger.log(Sheet.getRange("A2").getDataValidation().getCriteria());
Logger.log(Sheet.getRange("A2").getDataValidation().getCriteriaValues());
}
Thanks in advance.
By now, some functions like getDataValidation() are not working anymore, instead use:
function test()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var option = new Array();
option[0]="0";
option[1]="1";
option[2]="2";
option[3]="3";
var dv = SpreadsheetApp.newDataValidation();
dv.setAllowInvalid(false);
dv.setHelpText("Some help text here");
dv.requireValueInList(option, true);
sheet.getRange("A3").setDataValidation(dv);
}
I found the answer finally in issue 2680
the working code
function test()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var option = new Array();
option[0]="0";
option[1]="1";
option[2]="2";
option[3]="3";
var dv = sheet.getRange("A3").getDataValidation();
dv.setAllowInvalidData(false);
dv.setHelpText("Some help text here");
dv.setCriteria(SpreadsheetApp.DataValidationCriteria.ITEM_IN_LIST,true,option );
sheet.getRange("A3").setDataValidation(dv);
}
Working example from 2021:
function set_data_validation() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dest_range = ss.getActiveSheet().getRange('A1:A10');
var list_range = ss.getSheetByName('List').getRange('A1:A');
var rule = SpreadsheetApp.newDataValidation()
.requireValueInRange(list_range)
.setAllowInvalid(false)
.build();
var rules = dest_range.getDataValidations().map(x=>[rule]);
dest_range.setDataValidations(rules);
}
It will set data validation 'List from a range' for cells 'A1:A10' on current sheet. The range is column 'A' on a sheet with name 'List'.
Related
I'm trying to show the values from O1 until O28 but I am only getting from O1 using the code below.
function TEST() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange('O1:O28');
var value = range.getValue();
Browser.msgBox(value);
};
Any suggestions please?
function TEST() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange('O1:O28');
var vs = range.getDisplayValues()[0];
Browser.msgBox(vs.join(','));
};
Try using .getValues instead of .getValue.
Documentation for .getValue
Documentation for .getValues.
I believe this will be a super easy question for you. I downloaded a YT script to automatically update YT views on Google Sheets which you can find here (https://developers.google.com/gsuite/solutions/youtube-tracker).
I have a spreadsheet with 12 tabs and the code has a function to go through every tab in the spreadsheet.
function markVideos() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// Runs through process for each tab in Spreadsheet.
sheets.forEach(function(dataSheet) {
var tabName = dataSheet.getName();
var range = dataSheet.getDataRange();
var numRows = range.getNumRows();
var rows = range.getValues();
var headerRow = rows[0];
I want to change it to apply to a specific tab only, let's say "Sheet12", instead of running it for every tab.
function markVideos() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet12");
// Runs through process for each tab in Spreadsheet.
function NameOfTheFunction(dataSheet) {
var tabName = dataSheet.getName();
var range = dataSheet.getDataRange();
var numRows = range.getNumRows();
var rows = range.getValues();
var headerRow = rows[0];
I tried this but had no luck because it doesn't seem to be applying the function to the specific sheet. I believe this is super easy fix but I have no idea about programming.
Remove the forEach statement and use getSheetByName instead of getSheets:
function markVideos() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tabName = 'Sheet12';
var dataSheet = ss.getSheetByName(tabName)
var range = dataSheet.getDataRange();
var numRows = range.getNumRows();
var rows = range.getValues();
var headerRow = rows[0];
}
Array ForEach() Method calls a function once for each element in an array. That is why a function must be defined that will take an array element function(dataSheet)
Since you only need to access 1 sheet based on its sheet name.
You don't need to define a function to access the properties of that sheet.
Sample:
Sample Code:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheetByName("Sheet1");
var tabName = sheets.getName();
var range = sheets.getDataRange();
var numRows = range.getNumRows();
var rows = range.getValues();
var headerRow = rows[0];
Logger.log(tabName);
Logger.log(numRows);
Logger.log(rows);
Logger.log(headerRow);
}
Logger Log Output:
References:
Class Sheet Methods
SpreadsheetApp Methods
I'm trying to copy the values from one sheet :
https://docs.google.com/spreadsheets/d/1UDWk1xGnnL6qH_l5vo2MMwGH8wyXu0D9DCQMmZEKUJ0/edit#gid=0
To another :
https://docs.google.com/spreadsheets/d/1dWYj2W3gTDnNuqgX9O9RS0WWL02aDUQAVl1HJ5jTtvE/edit#gid=595677628
I tried the code there, but when I run it, nothing happens. I'm new to programmation, so maybe it's something vary basic.
function myFunction() {
function transferList() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OVERVIEW");
var sourceData = sourceSheet.getDataRange().getValues();
var targetSS = SpreadsheetApp.openById("1dWYj2W3gTDnNuqgX9O9RS0WWL02aDUQAVl1HJ5jTtvE").getSheetByName("RAWDATA");
var targetRangeTop = targetSS.getLastRow();
targetSS.getRange(targetRangeTop+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}
}
I would like the values to be copied at the last available row...
function transferList() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OVERVIEW");
var sourceData = sourceSheet.getDataRange().getValues();
var targetSS = SpreadsheetApp.openById("1dWYj2W3gTDnNuqgX9O9RS0WWL02aDUQAVl1HJ5jTtvE").getSheetByName("RAWDATA");
var targetRangeTop = targetSS.getLastRow();
targetSS.getRange(targetRangeTop+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}
Trying to copy data from input sheet to output sheet transposed. First time to copy between sheets, not very experienced. I have tried many methods to do the copyTo line of code looking at previous similar stackoverflow reports.
Error "Missing ; before statement. (line 11, file "Code")
Line 11 is last line, the inRng.copyTo ...
function fetchData() {
//Browser.msgBox("Test Button");
var inData = ("FUNGRAPH9");
var outData = ("Data");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var input = ss.getSheetByName(inData);
var output = ss.getSheetByName(outData);
//test getting data for 1st chart
inRng = input.getRange(10,3,1,7);
outRng = output.getRange(6,21,12,21);
inRng.copyTo(outData,6,21,12,21).CopyPasteType.transposed);
}
Revised code as Tanaike answered. It works now. thanks!
function fetchData() {
//Browser.msgBox("Test Button");
var inData = ("FUNGRAPH9");
var outData = ("Data");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var input = ss.getSheetByName(inData);
var output = ss.getSheetByName(outData);
//test getting data for 1st chart
inRng = input.getRange(10,3,1,7);
outRng = output.getRange("U6");
inRng.copyTo(outRng, SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true);
}
I used Tanaike's answer (modified for my corrected case) and it worked, Corrected code:
function fetchData() {
//Browser.msgBox("Test Button");
var inData = ("FUNGRAPH9");
var outData = ("Data");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var input = ss.getSheetByName(inData);
var output = ss.getSheetByName(outData);
//test getting data for 1st chart
inRng = input.getRange(10,3,1,7);
outRng = output.getRange("U6");
inRng.copyTo(outRng, SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true);
}
I need to copy a sheet on google SpreadSheet to another SpreadSheet document.
I´ve done my research, and I found two methods that do this, however both have problems that I don´t know how to fix.
The first method copies the sheet with format, but it keeps the referenced cells, so I get a reference error in the new document (#ref). I need a function that copies the format and the values (not references).
function copySheet() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheets()[0];
var destination = SpreadsheetApp.openById("15ucPbZrIYXZAOCYVdpK6OA0oyQT1NcsmuiJmDRfdpHQ");
sheet.copyTo(destination);
}
This second method copies the values without references, however it copies only values, without format.
function copySheetValues()
{
var source = SpreadsheetApp.getActiveSheet();
var sourcename = source.getSheetName();
var sourceDataRange = source.getDataRange();
var sourceSheetValues = sourceDataRange.getValues();
var sourceRows = sourceDataRange.getNumRows();
var sourceColumns = sourceDataRange.getNumColumns();
var destination = SpreadsheetApp.openById('15ucPbZrIYXZAOCYVdpK6OA0oyQT1NcsmuiJmDRfdpHQ');
destination.insertSheet(sourcename, 0);
destination.getDataRange().offset(0, 0, sourceRows, sourceColumns).setValues(sourceSheetValues);
}
How do I write a function that keeps the format and copies the values?
Since you seem to know how to get and set values using the whole data range, just use the other methods to get and set all the other parameters.
The script editor autocomplete is a great help in this case to try not to forget one !
I hope the list is complete, it is a bit of a pain to write though !.
code below, if one of them is not useful to you just delete it (in both directions (set and get).
function copySheetValues(){
var source = SpreadsheetApp.getActiveSheet();
var sourcename = source.getSheetName();
var sValues = source.getDataRange().getValues();
var sBG = source.getDataRange().getBackgrounds();
var sFC = source.getDataRange().getFontColors();
var sFF = source.getDataRange().getFontFamilies();
var sFL = source.getDataRange().getFontLines();
var sFFa = source.getDataRange().getFontFamilies();
var sFSz = source.getDataRange().getFontSizes();
var sFSt = source.getDataRange().getFontStyles();
var sFW = source.getDataRange().getFontWeights();
var sHA = source.getDataRange().getHorizontalAlignments();
var sVA = source.getDataRange().getVerticalAlignments();
var sNF = source.getDataRange().getNumberFormats();
var sWR = source.getDataRange().getWraps();
var destination = SpreadsheetApp.openById('15ucPbZrIYXZAOCYVdpK6OA0oyQT1NcsmuiJmDRfdpHQ');
var destinationSheet = destination.insertSheet(sourcename, 0);
destinationSheet.getRange(1,1,sValues.length,sValues[0].length).setValues(sValues)
.setBackgrounds(sBG)
.setFontColors(sFC)
.setFontFamilies(sFF)
.setFontLines(sFL)
.setFontFamilies(sFFa)
.setFontSizes(sFSz)
.setFontStyles(sFSt)
.setFontWeights(sFW)
.setHorizontalAlignments(sHA)
.setVerticalAlignments(sVA)
.setNumberFormats(sNF)
.setWraps(sWR);
}
Edit :
Bryan's answer and your comment made me think of another solution, much more simple and that handles merged cells as well. Here is the code :
function copySheetValuesV2(){
var source = SpreadsheetApp.getActiveSheet();
var sourceName = source.getSheetName();
var sValues = source.getDataRange().getValues();
var destination = SpreadsheetApp.openById('15ucPbZrIYXZAOCYVdpK6OA0oyQT1NcsmuiJmDRfdpHQ');
source.copyTo(destination)
var destinationSheet = destination.getSheetByName('Copy of '+sourceName)
destinationSheet.getRange(1,1,sValues.length,sValues[0].length).setValues(sValues);// overwrite all formulas that the copyTo preserved
}
In both script be sure that the destination sheet name does not already exist. I didn't handle that case although it is quite easy to do with a try/catch structure.
Another route to try that I think Serge was alluding to here.
function myFunction() {
var source = SpreadsheetApp.openById('SOURCE_ID');
var sourceSheet = source.getSheetByName('Sheet1');
var sourceRange = sourceSheet.getDataRange();
var sourceValues = sourceRange.getValues();
var tempSheet = source.getSheetByName('temp');
var tempRange = tempSheet.getRange('A1');
var destination = SpreadsheetApp.openById('DEST_ID');
sourceRange.copyTo(tempRange); // paste all formats?, broken references
tempRange.offset(0, 0, sourceValues.length, sourceValues[0].length)
.setValues(sourceValues); // paste all values (over broken refs)
tempSheet.copyTo(destination); // now copy temp sheet to another ss
}
This is a variation upon Sergei's answer.
This script will copy all visible sheets and export them in to a new spreadsheet with " Final" appended to the document title.
function copySheetValuesV4(){
var sourceSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheets = sourceSpreadsheet.getSheets();
var destination = SpreadsheetApp.create(sourceSpreadsheet.getName()+' Final');
for (var i = 0; i < sourceSheets.length; i++){
var sourceSheet = sourceSheets[i];
if (!sourceSheet.isSheetHidden()) {
var sourceSheetName = sourceSheet.getSheetName();
var sValues = sourceSheet.getDataRange().getValues();
sourceSheet.copyTo(destination)
var destinationSheet = destination.getSheetByName('Copy of '+sourceSheetName).setName(sourceSheetName);
destinationSheet.getRange(1,1,sValues.length,sValues[0].length).setValues(sValues);// overwrite all formulas that the copyTo preserved */
}
destination.getSheetByName("sheet1").hideSheet() // Remove the default "sheet1" */
}
}