I have been able to append a new row of data from an imported and live updating postgres import on google sheets. My requirement is to get the data to append in a new column, next to the one previous.
// function to save data
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var count = sheet.getRange('Sheet1!A3').getValue();
var date = sheet.getRange('Sheet1!B3').getValue();
sheet.insertColumnAfter(sheet.getMaxColumns());
sheet.getRange(1,sheet.getLastColumn(),2).setValues([count,date]);
}
Im getting the error of not being able to turn an object into an array - any help?
function append() {
var tabLists = "append"; //my sheet is named as "append"
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabLists);
sheet.clear()
var date = new Date('October 11, 2018 09:15:00')
var sr = 4
var sft = 1
var sc = 1
var nr = 1
var nc = 1
for(var i=0; i<5;i++){ //I'm appendind 5 columns for now.
sheet.getRange(sr,sc,nr,nc)//(start row, start column, number of rows, number of
columns)
.setValues([[
date
]]);
sc = sc + 1
}
}
Related
Basically I want to filter this sheet
by column J "company" and copy paste to several sheets.
[Data Sheet]
For example, the script will filter by Column J - KMA and it will populate into this Testing sheet 1 - KMA
[Testing Sheet 1]
Then the script will filter by Column J - LOL and it will populate into this Testing Sheet 2 - LOL
[Testing Sheet 2]
Whenever I populate the testing sheets, it will key in the current date in column A.
I tried using this code and it works. But they are not appending to a new empty row.... and it loads pretty slow
function FilterByKMA() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var test1 = SpreadsheetApp.openById("Sheet").getSheetByName("Sheet"); // Actual Sheet
const range = ss.getRange("A:X");
var checkfilter = range.getFilter();
const filter = range.createFilter();
const Filter_Criteria = SpreadsheetApp.newFilterCriteria().whenTextContains(["KMA"]);
const coll1 = 10; //
const add_filter = filter.setColumnFilterCriteria(coll1,Filter_Criteria);
Logger.log("Filter has been added.");
for(var i = 2; i <= ss.getLastRow();i++){
if(ss.getRange(i,10).getDisplayValue()=="KMA")
{
var tid = ss.getRange(i,1).getDisplayValue();
console.log(tid)
var last_scan_type = ss.getRange(i,6).getDisplayValue();
var last_scan_date = ss.getRange(i,7).getDisplayValue();
var last_scan_name = ss.getRange(i,9).getDisplayValue();
var maincon = ss.getRange(i,10).getDisplayValue();
// var to = ss.getRange(i,).getDisplayValue();
var cxname = ss.getRange(i,14).getDisplayValue();
var cxcontact = ss.getRange(i,15).getDisplayValue();
var to_address = ss.getRange(i,13).getDisplayValue();
var cod_cost = ss.getRange(i,17).getDisplayValue();
var timeZone = Session.getScriptTimeZone();
var email = Session.getActiveUser().getEmail();
test1.appendRow([Utilities.formatDate(new Date(), timeZone, "dd-MMM-yyyy"),email,,tid,,,,,,to_address,cxname,cxcontact,,,,last_scan_type,last_scan_date,last_scan_name,cod_cost])
}
}
filter.remove();
}
Trying to copy a range (L2:L9) to another spreadsheet based on a data validation selection (ID #) in a specific cell (G11) on the source spreadsheet.
The ID#s are all in alphanumeric order on the target spreadsheet along row 5, so all I need is to get the column number that corresponds to the choice made in the data validation field.
Before I had to convert the ID#s to Alphanumeric values, I simply had numeric values and the script worked fine. Now I'm having trouble converting the script to work with this modification.
function Submit() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // ss = source spreadsheet
var source_sheet = ss.getActiveSheet();
if (source_sheet.getName() == "Pre-Season CC") {
var SRange = source_sheet.getRange('L2:L9');
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
var target = SpreadsheetApp.openById('1MpKdxFyBrVQT3EumePQvtCZpgzcDW5xlYe4B1DKZCA8');
var target_sheet = target.getSheetByName('Pre-Season');
var idNumber = source_sheet.getRange('Pre-Season CC!G11').getValue();
for (var i = 0; i < idNumber.length; i++) {
for (var j = 0; j < idNumber.length;j++){
if (target_sheet[4][j] == idNumber){
Logger.log((j+1))
return j+1;
target_sheet.getRange('B6:B13').offset(0,valueA1 = ss.getRange('Pre-Season!G11').getValue(idNumber)+1).setValues(SData);
}
}
}
}
At that point, I need the data copied from source spreadsheet onto the target spreadsheet in it's designated column, according to the ID#. Please help!
Try below code.
function Submit() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // ss = source spreadsheet
var source_sheet = ss.getActiveSheet();
if (source_sheet.getName() == 'Pre-Season CC') {
// source sheet items
var SData = source_sheet.getRange('L2:L9').getValues();
var idNumber = source_sheet.getRange('Pre-Season CC!G11').getValue();
// target sheet items
var targetSS = SpreadsheetApp.openById('1MpKdxFyBrVQT3EumePQvtCZpgzcDW5xlYe4B1DKZCA8');
var target_sheet = targetSS.getSheetByName('Pre-Season');
// 5 = row where IDs are
var TData = target_sheet.getRange(5, 1, 1, target_sheet.getLastColumn()).getValues()[0];
// column index of #ID in target sheet
var colIndex = TData.indexOf(idNumber) + 1;
// write data in that column, rows from 6 to 13
target_sheet.getRange(6, colIndex, SData.length, 1).setValues(SData);
}
}
I have a script setup that moves all the records in a sheet into another sheet. However, I would like to add functionality to only move records that have the value "Approved" in a specific column (column I for my current situation). I've seen some other questions/answers to move individual rows based on values, but I haven't figured out how to leverage these scripts to run across a full spreadsheet. My guess is I can use a for loop but how?
Current script:
function MoveRecords(){
//Original Sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Example Input")
//Calculate # of Rows Ignoring Blank Array Values
var Rows = ss.getRange("A2:A").getValues();
var NumRows = Rows.filter(String).length;
/*Add a For Loop here to only get the rows with Approved in Column I?*/
//Parsed Data to Move
var rangeValues1 = sheet.getRange(2,1,NumRows,1).getValue();
var rangeValues2 = sheet.getRange(2,2,NumRows,5).getValues();
//Destination of Parsed Data
var DestinationSS = SpreadsheetApp.openById('ID');
var DestinationSheet = DestinationSS.getSheetByName('Approved');
var DestinationLastRow = DestinationSheet.getLastRow()+1;
//Move the Data
DestinationSheet.getRange(DestinationLastRow,3,NumRows,1).setValue(rangeValues1);
DestinationSheet.getRange(DestinationLastRow,5,NumRows,5).setValues(rangeValues2);
};
Any help is greatly appreciated!
You can get all values, filter every row for Approved then write those rows into destination sheet.
function MoveRecords() {
//Original Sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Example Input');
var values = sheet.getDataRange().getValues();
/*Add a For Loop here to only get the rows with Approved in Column I?*/
// col I = index 8
var res = values.filter(function(row) {
return row[8] == 'Approved';
});
//Parse Data to Move
var rangeValues1 = res.map(function(row) {
return [row[0]];
});
var rangeValues2 = res.map(function(row) {
row.shift();
while (row.length > 5) row.pop();
return row;
});
//Destination
var DestinationSS = SpreadsheetApp.openById('ID');
var DestinationSheet = DestinationSS.getSheetByName('Approved');
var DestinationLastRow = DestinationSheet.getLastRow() + 1;
//Move the Data
DestinationSheet.getRange(DestinationLastRow, 3, rangeValues1.length, 1).setValues(rangeValues1);
DestinationSheet.getRange(DestinationLastRow, 5, rangeValues2.length, 5).setValues(rangeValues2);
}
This is the first time I am programming using Google Script and I cannot figure out how to add a function to circumvent Google's error message of exceeding processing time. Could you please help?
The program gets the number of bars and pints ordered and references them into a different sheet which has a calculator to calculate costs and outputs the value. Would really appreciate the help
/**
*
*
*/
function STBIBOL()
{
var r = 6
var destclm = 11
var pintsclm = 105
var barsclm = 101
var spreadsheet_url = 'https://docs.google.com/spreadsheets/d/1cqgdo_pgJ4n0xhXqMah24g6jjv7sfl2o92k1pwXNUuo/edit#gid=532254312';
var sheet_name = 'BOLs Entered for Fulfillment';
var sheet_name1 = 'STBI Calculator';
var ss = SpreadsheetApp.openByUrl(spreadsheet_url);
var sheet = ss.getSheetByName(sheet_name);
var sheet1 = ss.getSheetByName(sheet_name1);
var lastRow = sheet.getLastRow();
for(i =1; i< 20; i++)
{
var rangeD = sheet.getRange(r,destclm)
var dest = rangeD.getValue()
var rangeS = sheet.getRange(r,pintsclm)
var pints = rangeS.getValue()
var rangeB = sheet.getRange(r, barsclm)
var bars = rangeB.getValue()
var range = sheet1.getRange(14,3)
range.setValue(dest)
var range2 = sheet1.getRange(11, 3)
range2.setValue(pints)
var range3 = sheet1.getRange(12, 3)
range3.setValue(bars)
var rangev = sheet1.getRange(8,3)
var value = rangev.getValue()
var range4 = sheet.getRange(r,110)
range4.setValue(value)
r = r + 1
}
}
Instead of making multiple calls to GAS services on each run of the loop, which causes the execution to fail, you could simply get the entire range in one call like this:
var range1 = sheet1.getRange(startRow, startColumn, numberOfRows, numberOfColumns);
If the range in the 2nd sheet has the same dimensions (same number of rows and columns) as the 1st sheet, you could simply 1) get the values from the original sheet as a 2d array
var values1 = range.getValues();
2) Write values to the 2nd sheet
var range2 = sheet2.getRange(startRow, startColumn, numberOfRows, numberOfColumns);
range2.setValues(values1);
I need to create a master sheet for our managers that only include activity happening in their areas (I have a script for this that does what I need). These sheets are populated from my master sheet (and are not in the same workbook).
I need to have rows copy to the applicable sheet when there is a new submission. My code below dumps them all to one sheet and I'm not sure why. I'm assuming I don't have the right condition but how do you say "move rows to this sheet when it's this manager"? I thought calling the sheet ID by row would be enough, but I guess not.
Would I then have to do an if else statement for every manager? That will get tedious since I have about 80 managers.
Below is my current code. Any help or advice is appreciated!
function moveRows(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("Form Responses 1");
var data = sh.getDataRange().getValues();
// Grab the Headers
var headers = sh.getRange(1,1,1,sh.getLastColumn()).getValues();
var nameCol = headers[0].indexOf('Enter Your Name');
var candCol = headers[0].indexOf('Choose Candidate');
var typeCol = headers[0].indexOf('Merged Experience');
var docCol = headers[0].indexOf('Document Link');
var timeCol = headers[0].indexOf('Timestamp');
var subjectCol = headers[0].indexOf('Choose the Content Area');
var ratingsCol = headers[0].indexOf('Ratings Summary');
var accessCol = headers[0].indexOf('Access');
var activityCol = headers[0].indexOf('Copied to Activity Sheet');
var masterCol = headers[0].indexOf('Master Activity Sheet');
var target = [];
// Loop over the values line by line
for (var i = 0; i < data.length; i++) {
var row = data[i];
var name = row[nameCol];
var activity = row[activityCol];
var master = row[masterCol];
var candidate = row[candCol];
var type = row[typeCol];
var subject = row[subjectCol];
var ratings = row[ratingsCol];
var access = row[accessCol];
var guide = row[docCol];
var time = row[timeCol];
if (activity == "") { // if condition is true copy the whole row to target
target.push([time,name,candidate,subject,type,guide,ratings]);
Logger.log(target);
var ss2 = SpreadsheetApp.openById(master);
var sh2 = ss2.getSheetByName("Sheet1");
sh2.getDataRange().offset(sh2.getLastRow(), 0, target.length, target[0].length).setValues(target);
}
}
}
Note: I have a bunch of different scripts that move rows from one tab to another, but I couldn't find anything where I move it across multiple workbooks.
What you can do is to have an array with for each manager the sheet where to copy the data.
var managerSheet = {
"manager1":"SheetAzerty",
"manager2":"SheetQwerty"
};
to select the sheet just do :
var manager = "manager1"; //This is the value you retrieve
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(managerSheet[manager]);
Stéphane