I'm running API requests to export data from third party to a Google Spreadsheet.
This script will be running automatically on a weekly basis (for report purposes).
In this Google Spreadsheet, I need to:
Create columns across all the sheets (they are all structured in the same way).
Transpose the data from previous week to the newly created columns.
Delete the old data from last week in the sheet named 'raw data'
Hide this sheet name 'raw data'
I've created a script with the 4 tasks I need Google Sheet to perform.
I'm guessing there are some issues with my functions/constants in my following script. I just need one function that nests all the other needed 'subfunctions' inside.
A bit of help to fix this would be appreciated. I feel I'm almost there :)
function columnCreation() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheets()[0,1,2,3,4,5];
const range = sheet.getRange('D1:H55').getValues();
const newrange = sheet.getRange('I1:M55');
const rules = sheet.getConditionalFormatRules();
const today = Utilities.formatDate(new Date(), "GMT+7", "MM/dd/yyyy");
sheet.insertColumnsAfter(8,5);
sheet.clearConditionalFormatRules();
sheet.setConditionalFormatRules(rules);
newrange.setValues(range);
sheet.getRange('D1').setValue(today).setNumberFormat("MMMM DD");
}
function clearAllContent() {
var range = SpreadsheetApp.getActive().getSheetByName('Raw Data').getRange();
range.clearContent();
}
function hideSheet() {
SpreadsheetApp.getActive().getSheetByName('Raw Data').hideSheet();
}
As an answer for the comment:
The error shown is on line 4 which refers to line 3 var sheets which is not a single sheet but an array of them.
To solve it you have to select which of the sheets you want to select. As an example, if there is only one Sheet, you have to choose 0:
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheets()[0];
const range = sheet.getRange('D1:H55').getValues();
Related
I am in need of assistance with google sheet app script in accomplishing the task of highlighting & copying cells A2:K12 with its current cell formats from Sheet A to another sheet's (Sheet B) last available row. Can someone help me in getting this task complete in a function? I have googled some scripts, but they are not what I am looking for, the ones that I have found are only copying values and I would like to copy with the cell formats as it has color highlights and all in my sheet.
Copy from Sheet to Sheet in same Spreadsheet
function myfunk() {
const ss = SpreadsheetApp.getActive();
const sha = ss.getSheetByName("SheetA");
const shb = ss.getSheetByName("SheetB");
sha.getRange("A2:K12").copyTo(shb.getRange(shb.getLastRow() + 1, 1))
}
copyTo
If you simply use copyTo method on a range you would like to copy, it will copy everything including the formatting. You would need to be specific if you like to copy just values. It is similar to just copy/pasting without the script.
Here is a sample code I wrote to copy data with colored backgrounds in B2:B5 in sheet 1 and copies it to sheet 2 as is.
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sheet1 = ss.getSheetByName('Sheet1');
const sheet2 = ss.getSheetByName('Sheet2');
const copyFromRange = sheet1.getRange("B2:B5");
const copyToRange = sheet2.getRange("B2:B5");
copyFromRange.copyTo(copyToRange)
}
You should look in copyTo explaination which has other attributes that you can specify under copypastetype.
I'm new to Apps Script and don't know how I can get my selected cells copied into another worksheet.
I want the following but instead of copying A7:A9 into a new worksheet i want the cells/range that are selected at the time to be copied. the selected cells/range are going to be different each time that i want to use the script.
var rangeList = SpreadsheetApp.getActiveRangeList();
function TestMakro() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A7:A9').activate();
spreadsheet.insertSheet(1);
spreadsheet.getRange('Tabellenblatt1!A7:A9').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
thanks
So instead of using SpreadsheetApp.getActive() it's better to define separately the spreadsheet and the active range. you can then get A1 annotation of the active range to find the same area in the new sheet (I assumed that is what you are trying to achieve). Key here is that even if you create a new sheet, it doesn't become Active. Hence you still need to find the same range in it.
Here is the code:
function TestMakro() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const activeRange = spreadsheet.getActiveRange();
const newSheet = spreadsheet.insertSheet(1)
activeRange.copyTo(newSheet.getRange(activeRange.getA1Notation()) , SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false)
}
I have been trying to filtering the columns using Google Apps Script but i am unable to get that how the filter will work for second criteria.
I want to create a filter which will work for entire data validation that is available in the attached sheet.
If i selected the position from cell C2 Head Of Product Design then position column will be shown rows with Head Of Product Design and then i select the Stage Name from cell D2 Profile review. then two filter will be created 1 for C2 and other for D2 and same goes for all.
Your help will be much appreciated.
However first onEdit is working but second is not working i do not know why. Then i need to create filters ahead. But i am stuck on second 1.
Your help will be appreciated.
Sheet
function onEdit() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet1 = ss.getSheetByName("Sheet1");
const value = sheet1.getRange('C2').getValue();
const range = sheet1.getRange("A5:T");
const filter = sheet1.getFilter();
if (filter) {
filter.remove();
}
range.createFilter().setColumnFilterCriteria(2, SpreadsheetApp.newFilterCriteria().whenTextContains(value).build());
}
function onEdit2() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet1 = ss.getSheetByName("Sheet1");
const value = sheet1.getRange('D2').getValue();
const range = sheet1.getRange("A5:T");
const filter = sheet1.getFilter();
if (filter) {
filter.remove();
}
range.createFilter().setColumnFilterCriteria(4, SpreadsheetApp.newFilterCriteria().whenTextContains(value).build());
}
In your situation, how about the following modification?
Modified script:
function onEdit() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet1 = ss.getSheetByName("Sheet1");
const [c2, d2] = sheet1.getRange('C2:D2').getValues()[0];
const range = sheet1.getRange("A5:T");
const filter = sheet1.getFilter();
if (filter) {
filter.remove();
}
range.createFilter()
.setColumnFilterCriteria(2, SpreadsheetApp.newFilterCriteria().whenTextContains(c2).build())
.setColumnFilterCriteria(4, SpreadsheetApp.newFilterCriteria().whenTextContains(d2).build());
}
In this modified script, your onEdit and onEdit2 are merged.
Added:
From your following new question,
I have multiple sheets with different data but each sheet has same data validation cells and Columns where filter will apply. So your above code is being used in Sheet1, then i add same code for Sheet2 But Sheet2 code is not working. I did not changed the code name that is onEdit for both sheets
When the Sheet1 is edited script should work for Sheet1 if Sheet2 is added scripts should work for Sheet2
In this case, how about the following sample script?
Sample script:
function onEdit() {
const sheets = ["Sheet1", "Sheet2"];
const sheet = SpreadsheetApp.getActiveSheet();
if (!sheets.includes(sheet.getSheetName())) return;
const [c2, d2] = sheet.getRange('C2:D2').getValues()[0];
const range = sheet.getRange("A5:T");
const filter = sheet.getFilter();
if (filter) {
filter.remove();
}
range.createFilter()
.setColumnFilterCriteria(2, SpreadsheetApp.newFilterCriteria().whenTextContains(c2).build())
.setColumnFilterCriteria(4, SpreadsheetApp.newFilterCriteria().whenTextContains(d2).build());
}
When this script is run, when the active sheet is Sheet1 and Sheet2, the script is run.
From OP's previous question, I understood that OP wants to achieve this without using the event object. So I proposed the above sample scripts.
function transferDataToDataBase(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var DB = SpreadsheetApp.openById("SHEET ID");
var SCPM_Input = ss.getSheetByName("Report")[0];
var history_Input = DB.getSheetByName(Database);
var blankRow = history_Input.getLastRow();
SCPM_Input.getRange(("A2:T").copyTo(history_Input.getRange(blankRow+1,1,blankRow+1,20));
}
Question:
The last line has an error at ";", don't know how to fix it. It said the syntax error;
It seems copy To only works for transferring from one sheet to another in the same spreadsheet, but not in between spread sheet. Is there a way to copyTo between spreadsheets?
Many thanks.
If you want to copy the formatting as well then copy the entire sheet/tab and then use copyTo() to copy the range and then delete the copied sheet. Or if you just want the data use getValues() and setValues();
This is part of the problem:
var SCPM_Input = ss.getSheetByName("Report")[0]; because the index zero at the end should not be there and you cannot have sheet.getLastRow() + 1 number of rows in a sheet as shown in the next row.
SCPM_Input.getRange(("A2:T").copyTo(history_Input.getRange(blankRow+1,1,blankRow+1,20));
And copyTo doesn't work between spreadsheets.
There is an update:
Since the database will be used by multiple users. I set up a standalone database. I copied my "Report" sheet from spreadsheet 1 to spreadsheet 2. It will create a sheet "copy of Report".I rename, copied to Database sheet and delete it.
Feel free to discuss if there is an easier way to do it. Thanks.
// transfer the report to DataBase Spreadsheet
function transferDataToDataBase(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var db = SpreadsheetApp.openById('spreadsheet ID')
var scpm_Input = ss.getSheetByName("Report");
scpm_Input.copyTo(db);
Utilities.sleep(3000);
db.getSheetByName('copy of report').setName('Import')
var history_Input = db.getSheetByName('Database');
var blankRow = history_Input.getLastRow();
var target = db.getSheetByName('Import');
target.getRange("A2:T").copyTo(history_Input.getRange(blankRow+1,1,blankRow+1,20));
history_Input.getRange(blankRow+1,21).setValue(new Date()).setNumberFormat("yyyy-mm-dd h:mm") //update the date and time
db.deleteSheet(target);
}
I am very new to this whole scripting business and I am looking for some advice to solve my problem.
I try to copy data from a column range (H3:H10) in Sheet 1 (Search) to the first free row in sheet 2 (Data). I found this piece of code (from Kos, written 2017), which I thought would do the trick. Unfortunately I can't figure out how to change the code, so it would write the data into the first free row instead of writing the whole data in 1 column in the first free row.
Anyone out here who can help me?
function copyPaste()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Search");
var pasteSheet = ss.getSheetByName("Data");
// get source range
var source = copySheet.getRange(3,8,8,1);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,1);
// copy values to destination range
source.copyTo(destination);
// clear source values
source.clearContent();
}
Copy H3:H to First Empty Row in Sheet2
function copyColToRow() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');
const vs=sh.getRange(3,8,sh.getLastRow()-2,1).getValues().map(function(r){return r[0];});
const osh=ss.getSheetByName('Sheet2');
osh.getRange(osh.getLastRow()+1,1,1,vs.length).setValues([vs]);
}
Spreadsheet Ref
Array.map method