I've a list of rows automatically updated and I would like to copy and save them on daily basis.
I've wrote the following script :
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var rowcount = sheet.getRange("A1:A").getValues();
var sheetdef = ss.getSheets()[1];
for(var i = 0; i < rowcount.length; i++){
var weeknum = sheet.getRange('Feed!A1').getValue();
var year = sheet.getRange('Feed!B1').getValue();
var date = sheet.getRange('Feed!C1').getValue();
var sessions = sheet.getRange('Feed!D1').getValue();
sheetdef.appendRow([weeknum,year,date,sessions]);
}}
but as you will see it never stop running, plus it only copy the data from the first row. if I'm able to identify my issue I don't really know how to solve it.
I assume that my rowcount line if wrong since it also count blank row. I also need to update sheet.getRange formula so they take dynamic argument.
thanks !
Untested but should work:
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
//var rowcount = sheet.getRange("A1:A").getValues();
var Avals = ss.getRange("A1:A").getValues();
var rowcount = Avals.filter(String).length; //This will take the last non blank cell from A column
var sheetdef = ss.getSheets()[1];
//I suggest you define Feed sheet as well
var feedsheet = ss.getSheetsByName("Feed")
for(var i = 0; i < rowcount.length; i++){
var weeknum = feedsheet(i,1).getValue(); //<- ith row and first/second column
var year = feedsheet.getRange(i,2).getValue();
var date = feedsheet.getRange(i,3).getValue();
var sessions = feedsheet.getRange(i,4).getValue();
sheetdef.appendRow([weeknum,year,date,sessions]);
}}
With the help of Vasim, I've come up with this:
// function to save data Tendance sessions
function saveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var sheetdef = ss.getSheets()[1];
lastRow = sheet.getLastRow();
for (i = lastRow; i > 0; i--){
var weeknum = sheet.getRange(i,1).getValue();
var year = sheet.getRange(i,2).getValue();
var date = sheet.getRange(i,3).getValue();
var sessions = sheet.getRange(i,4).getValue();
sheetdef.appendRow([weeknum,year,date,sessions]);
}}
Related
I don't need empty how I should do, I think problem in lastrow funtion (see in picture)
I want to create pdf with can select area and select sheet without hide other sheet is posible ?
this is code(copy from some youtube)
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var shtprint = ss.getSheetByName("Test print");
var shtkishu = ss.getSheetByName('proplan');
var shmaster = ss.getSheetByName('Master')
var shsetting = ss.getSheetByName('SETTING')
var shdata = ss.getSheetByName('DATA')
var shzttpp = ss.getSheetByName('ZTTPP038')
var LastRow = shtkishu.getRange(5,8).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var kishu = shtkishu.getRange(5,8,+LastRow).getValues().flat().map(v=>[v.slice(-7)]);
Logger.log(kishu)
for (var i = 0;i < kishu.length;i++){
shtprint.getRange('E2').setValue(kishu[i][0]);
var name = shtprint.getRange('E2').getValue();
shtkishu.hideSheet();
shmaster.hideSheet();
shsetting.hideSheet();
shdata.hideSheet();
shzttpp.hideSheet();
var folder = DriveApp.getFoldersByName("test").next();
var Blob = shtkishu.getParent().getBlob().getAs('application/pdf');
folder.createFile(Blob).setName(name);
}
shtkishu.showSheet();
}
don't know how to fix that
I have one sheet for a group of people. I'm trying to make a script so that whenever they edit the sheet, the time that they last edited will show up in a sheet in another document.
A sample row from the group sheet looks like this:
col1|...|col10|person name|...|col17
and the sheet that the time should be going to looks like this:
person name|col2|...|col10|date last edited.
this is the code that I've been trying and it is not working so far:
function onEdit(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
var time = new Date().toLocaleString("en-US", {timeZone: "America/Toronto"})
var ID = "sheet ID"; //The ID for the second sheet goes here
var sheet = SpreadsheetApp.OpenById(ID);
var target = sheet.getSheetByName("Sheet2");
var lastColumn = target.getLastColumn();
var lastRow = target.getLastRow();
var range = target.getRange(1,1,lastRow, lastColumn);
var data = range.getValues();
var userEmail = e.user.getEmail();
var array = userEmail.split(".");
var firstName = array[0];
var secondSheet = e.source.getActiveSheet();
var lastRowSecond = secondSheet.getLastRow();
var lastColumnSecond = secondSheet.getLastColumn();
var secondRange = secondSheet.getRange(1,1,lastRowSecond, lastColumnSecond);
var secondData = secondRange.getValues();
var nameRow = secondData[row-1];
var nameColumn = nameRow[10];
var secondPersonarray = nameColumn.split(" ");
var secondPersonFirstName = secondPersonarray[0];
for (var i=1; i<lastRow; i++) {
var sheetRow = data[i];
var name = sheetRow[0].split(" ");
var firstNameSheet = name[0];
if ((firstName == firstNameSheet) && (firstName == secondPersonFirstName)) {
target.getRange(sheetRow, 10).setValue(time);
}
}
}
I've done something similar, only with one document, so I know the problem isn't with the time.
Quite new to JS, I'm willing to do some repetitive task within google sheet script editor.
The idea is to check all rows of tab1. For each row, if date in column D is older than 5 days prior to today, it transfers the full row to tab2 first empty row and make sure the row doesn't exist anymore in tab1. The idea is to archive data older than 5 days.
I could do this with VBA but I am very new to google sheet script and need the help of the community (I'm afraid I really don't have the basis of JS).
I will then add a time trigger (I know how to do this !) for this script.
EDIT:
from this script deleting :
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Foglio1");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
var currentDate = new Date();
var oneweekago = new Date();
oneweekago.setDate(currentDate.getDate() - 7);
for (i=lastrow;i>=2;i--) {
var tempdate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if(tempdate < oneweekago) { sheet.deleteRow(i); } } }
And this script moving data :
function moveCols() {
var ss = SpreadsheetApp.getActive();
var sourceSheet = ss.getSheetByName('Sheet1');
var destSheet = ss.getSheetByName('Sheet2');
sourceSheet.getRange('A:A').moveTo(destSheet.getRange('G1'))
sourceSheet.getRange('B:B').moveTo(destSheet.getRange('I1'))
sourceSheet.getRange('C:C').moveTo(destSheet.getRange('L1')) }
I was able to do the below :
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("input");
var destSheet = ss.getSheetByName("input archive");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
var currentDate = new Date();
var oneweekago = new Date();
oneweekago.setDate(currentDate.getDate() - 7);
for (i=lastrow;i>=2;i--) {
var tempdate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if(tempdate < oneweekago) {
sourceSheet.getRange('i:i').moveTo(destSheet.getRange('LASTAVAILABLEROW'));
} } }
But I have no idea how to paste to last row. Maybe this will work ?
var Avals = destSheet.getRange("A1:A").getValues();
var Alast = Avals.filter(String).length;
Haven't got time to write a full answer but hope this gives you some guidance
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("data");
var sheet2 = ss.getSheetByName("archive");
var data = sheet1.getRange("I1:I"+sheet.getLastRow()).getValues();
sheet2.getRange("A" + (sheet2.getLastRow()+1) + ":A" +sheet.getLastRow()).setValues(data);
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