having trouble with the following code:
function addDelivery() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName("Sheet7");
var range = srcSheet.getRange("B3:L3").getValues();
var formulas = srcSheet.getRange("B3:L3").getFormulas();
srcSheet.insertRowBefore(6);
srcSheet.getRange(6,2,1,11).setValues(range);
range.clearContents();
range.setFormulas(formulas);
I want to clear the contents of cells B3:L3 but keep the formula in L3.
I get the error message "TypeError: range.clearContent is not a function".
I read that clearContent is a method of Range and clearContents is a method of sheet.
I've tried both, but neither work.
Please help.
Best regards
manc
Perhaps this would work better:
function addDelivery() {
var ss = SpreadsheetApp.getActive();
var srcSheet = ss.getSheetByName("Sheet7");
var range=srcSheet.getRange("B3:L3");
var values = range.getValues();
var formulas = range.getFormulas();
srcSheet.insertRowBefore(6);
srcSheet.getRange(6,2,1,11).setValues(values);
range.setFormulas(formulas);
srcSheet.getRange("B3:K3").clearContent();
I thought about it some more and I think I'd probably do it this way:
function addDelivery() {
var ss = SpreadsheetApp.getActive();
var srcSheet = ss.getSheetByName("Sheet7");
var range = srcSheet.getRange("B3:L3");
var values = range.getValues();
var formula = srcSheet.getRange("L3").getFormula();
srcSheet.insertRowBefore(6);
srcSheet.getRange(6,2,values.length,values[0].length).setValues(values);
srcSheet.getRange("L3").setFormula(formula);
srcSheet.getRange("B3:K3").clearContent();
Related
I want eliminate formula information that is in an array of cells. My idea was to get the values of the cells as an array and then set the values back into the same place. I can't get it to work and have a feeling it due to my syntax. Any suggestions?
function getset() {
var ss = SpreadsheetApp.getActive();
var lr = sh.getLastRow();
var places = ss.getRange(2,11, lr-1).getValues();
ss.getRange(2,11, lr-1).setValues(places);
}
This works for me
function getset() {
var ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
var lr = sh.getLastRow();
var places = sh.getRange(2,11, lr-1).getValues();
sh.getRange(2,11, lr-1).setValues(places);
}
I am using the below code to copy my data from sheet1 to sheet2 and append the data in sheet2 if script runs again? The issue is that my range is not fixed in sheet 1,I want to copy whatever range contains the content should be copied to sheet2 Can anyone guide that what to change in the below code to get the result?
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("sheet1");
var pasteSheet = ss.getSheetByName("sheet2");
var source = copySheet.getrange(2,1,200,7);
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,200,7);
source.copyTo(destination, {contentsOnly:true});
var data = copySheet.getRange(2,1,200,6)
data.clearContent()
}
Method1:
Include:
var paste_lr = pasteSheet.getLastRow()
Modify:
var source = copySheet.getrange(paste_lr+1,1,200,7);
Similarly change 200 using same function.
Method2:
var sp = PropertiesService.getScriptProperties();
var SourceLr = sp.getProperty("Source_Data_Rows") || 0;
var source = copySheet.getrange(SourceLr+1,1,200,7);
var Source_Lr = copySheet.getLastRow();
sp.setProperty("Source_Data_Rows", Source_Lr);
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
Please excuse the length of the post I was getting help but the person has not been online for the last week so thought I would try here to get the last few kinks out. Thank you for any extra help.
Here is my original script.
function ImportDataRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("AM trip"); //To Sheet Name
var ssraw = SpreadsheetApp.openById("1n4**4HzvVS****RQhPHndUbn0N0nkpOE5NpO2U"); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName("AM trip"); // From Sheet name
var range = sheetraw.getRange("B9:U38");
var data = range.getValues();
sheet.getRange("C1").setValues(data)
}
My adjusted script:-
function ImportDataRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('AM trip'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('18h-UAy10EvANHZXLebspZsUbE2fisyEBAFnUYV9JBHs'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('AM trip'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getRange(sheet.getLastRow()+1,3).setValue(data);
}}
This takes B7 from the original spreadsheet and places it in the next blank row down and in Column C but not B7:U38, if i change setValue(data) to setValues(Data) I get the range height error.
Then below, I got from you and below the script is the error it produces:-
function ImportDataRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('AM trip'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('18hAy10EvANHZXLebspZsUbE2fisyEBAFnUYV9JBHs'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('AM trip'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getLastRow()+1;
setValues(sheet, lastRow, 3, data);
The reason for the error is range height should match the height of the data being input.
You will have to modify the code like so
function ImportDataRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('AM trip'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('18hAy10EvANHZXLebspZsUbE2fisyEBAFnUYV9JBHs'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('AM trip'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getLastRow()+1;
sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
}
Basically, get the length of the data array use array.length gives you number of rows. You find more details on it here
Details on setValues can be found here, similarly details on getRange can be found here
Hope that helps
I want to copy data from one spreadsheet into another one. But
1. The Data are on several sheets
2. Only certain Columns (just to speed it up, don't need all columns)
For the beginning I used the following script:
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('0AjN7uZG....'); // sss = source spreadsheet
var ss = sss.getSheetByName('Monthly'); // ss = source sheet
//Get full range of data
var SRange = ss.getDataRange();
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
var tss = SpreadsheetApp.openById('8AjN7u....'); // tss = target spreadsheet
var ts = tss.getSheetByName('RAWData'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
}
I took this one from user2741, which was posted here:
..this one works fine for one sheet.
But now I am unsure how to get the data from more sheets and actually sort it. Does anybody has a good idea on how to do this?
Thank you all,
Sascha
This one here works, if everything happens in the same spreadsheet
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SummarySheet").clear();
mergeSheet2("SummarySheet", "123");
mergeSheet2("SummarySheet", "345");
mergeSheet2("SummarySheet", "748");
mergeSheet2("SummarySheet", "293");
function mergeSheet2(targetSheetName, sourceSheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName(sourceSheetName);
var lastRow = sourceSheet.getLastRow();
var lastCol = sourceSheet.getLastColumn();
var source = sourceSheet.getRange(1,1,lastRow,lastCol); // Error after some iterations: The coordinates or dimensions of the range are invalid. (line 11, file "copy-m.js") -> might be empty spreadsheet
var destSheet = ss.getSheetByName(targetSheetName);
var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
source.copyTo(destRange, {contentsOnly: true});
}
but for some reason I don't get it run when using different spreadsheets
Error: "Target range and source range must be on the same spreadsheet"
I tried it with:
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SummarySheet").clear();
SpreadsheetApp.openById('ID');
mergeSheet2("SummarySheet", "123");
function mergeSheet2(targetSheetName, sourceSheetName) {
var ss = SpreadsheetApp.openById('ID');
var ssd = SpreadsheetApp.openById('ID');
var sourceSheet = ss.getSheetByName(sourceSheetName);
var lastRow = sourceSheet.getLastRow();
var lastCol = sourceSheet.getLastColumn();
var source = sourceSheet.getRange(1,1,lastRow,lastCol); // Error after some iterations: The coordinates or dimensions of the range are invalid. (line 11, file "copy-m.js") -> might be empty spreadsheet
var destSheet = ssd.getSheetByName(targetSheetName);
var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
source.copyTo(destRange, {contentsOnly: true});
Any ideas?
The solution is simpler than what you tried, since you want to copy values only and knowing that copyTo only works in the same spreadsheet, just get the values from source and paste it in destination like this:
function copyFromSourceToTarget(targetSheetName, sourceSheetName) {
var ss = SpreadsheetApp.openById('ID');
var ssd = SpreadsheetApp.openById('ID');
var sourceSheet = ss.getSheetByName(sourceSheetName);
var sourceData = sourceSheet.getDataRange().getValues();
var destSheet = ssd.getSheetByName(targetSheetName);
destSheet.getRange(destSheet.getLastRow()+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}
To make it more flexible I'd recommend to use 4 parameters and simplify again the code :
function copyFromSourceToTarget(targetSheetName,targetSsId, sourceSheetName,sourceSsId) {
var ss = SpreadsheetApp.openById(sourceSsId)getSheetByName(sourceSheetName);
var ssd = SpreadsheetApp.openById(targetSsId).getSheetByName(targetSheetName);
var sourceData = ss.getDataRange().getValues();
ssd.getRange(ssd.getLastRow()+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}