-can anyone help me check on this code because even the condition was met it still setting the value of all the data. The data should only be updated when the condition is met.
var ss = SpreadsheetApp.openById('id');
var sheet = ss.getSheetByName("VL Request");
var range = sheet.getDataRange();
var values = range.getValues();
var rid = [1];
// writes the form data to the spreadsheet
for (var i = 1; i <= values.length; i++) {
var sel = sheet.getRange(i, 2).getValue();
if (sel = ReqNum) {
sheet.getRange(i, 14).setValue(pri);
sheet.getRange(i, 16).setValue(stat);
sheet.getRange(i, 18).setValue(rem);
}
}
Try this:
function unknown() {
var ss=SpreadsheetApp.openById('id');
var sheet=ss.getSheetByName("VL Request");
var range=sheet.getRange(2,1,sheet.getLastRow()-1,18);
var values=range.getValues();
for(var i=0;i<values.length;i++) {
if(values[i][1]==ReqNum) {//First time through is row 2 column 2
sheet.getRange(i+2, 14).setValue(pri);//i+2 is the row, 14 is the column
sheet.getRange(i+2, 16).setValue(stat);//the first time through is row 2, column16
sheet.getRange(i+2, 18).setValue(rem);
}
}
}
Related
https://docs.google.com/spreadsheets/d/18odtLaamxJISS7Gq-N-xPxVu1Fk55AYQoFojkmIgxbM/edit#gid=0
function myFunction() {
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Messages from BOs");
var startRow = 4; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A4:C3
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var subject = row[1];
var message = row[2]; // Second column
MailApp.sendEmail(emailAddress, subject, message);
}
for (var i in data, i++) {
var row1 = data[i];
Browser.msgBox(data[i+1])
/**
var emailAddress1 = row[0]; // First column
var subject2 = row[1];
var message3 = row[2]; // Second column
*/
var ad = row1;
var ts = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test');
ts.getRange(startRow, i+1 ).setValue(row1);
//i = i + 1;
}
}
}
Here i wanted to update sheet2 every time when email send to "Recipient EMAL ID" to the new row of sheet2.
Please see my google sheet. I tried below code but it does not work well. If any who can help me in solve this problem.
Try to change:
ts.getRange(startRow, i+1 ).setValue(row1);
with:
ts.appenRow(row1);
https://developers.google.com/apps-script/reference/spreadsheet/sheet#appendRow(Object)
This is a lot simpler:
function sendEmails() {
const ss = SpreadsheetApp.getActive()
var sh = ss.getSheetByName("Messages from BOs");
var ts = ss.getSheetByName('Test');
var sr = 4; // First row of data to process
var rg = sh.getRange(sr, 1, 1, 3);
var vs = rg.getValues();
vs.forEach(row => {
MailApp.sendEmail(row[0], row[1], row[2]);
ts.appendRow(row);
});
}
I am having a google response sheet. I run this sheet every night at 12. whenever some response are received I want to append the row to another sheet and delete the respective row in form responses. Here I want to start appending from the oldest timestamp in form responses i.e. from row number two as row number 1 is questions. whenever Irun this code, I am successfully appending the row but not able to delete the row.
Here is the code I am trying to do:
//** Copying the response to another Spreadsheet for roster management**//
function copySheet(){
var source = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = source.getSheetByName("Form Responses 1");
var lastRow = sourceSheet.getLastRow();
var getData = sourceSheet.getDataRange().getValues();
var destSheet = source.getSheetByName("10017135ADC");
Logger.log(lastRow);
if(lastRow >1){
for (var j =1; j<lastRow; j++) {
// Logger.log(j);
var rowData = getData[j];
// Logger.log(rowData);
destSheet.appendRow(rowData);
sourceSheet.deleteRow(j);
}
}
}
Try this:
function copySheet(){
var source = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = source.getSheetByName("Form Responses 1");
var lastRow = sourceSheet.getLastRow();
var getData = sourceSheet.getDataRange().getValues();
var destSheet = source.getSheetByName("10017135ADC");
Logger.log(lastRow);
let d=0;//keep track of deleted rows
if(lastRow >1){
for (var j =1; j<lastRow; j++) {
// Logger.log(j);
var rowData = getData[j];
// Logger.log(rowData);
destSheet.appendRow(rowData);
sourceSheet.deleteRow(j-d++);
}
}
}
Cooper's new variable approach was good. But upwards for deletion is a better approach.
Try something like this
for (var i = getData.length - 1; i > 1; i--) {
//anything you want to perform
destSheet.appendRow(getData[i]);
sheet.deleteRow(i);
}
I wrote a piece of code to send automatically emails from a list in google sheet (which works), but when I want to add an Alias email, I cannot seem to make it work. Any help would be greatly appreciated. Here is the code:
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';
/**
* Sends non-duplicate emails with data from the current spreadsheet.
*/
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows =3; // Number of rows to process
// Fetch the range of cells A2:E3
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
// add the attachments variables
var contents = DriveApp.getFolderById("").getFiles();
var attachments = [];
while (contents.hasNext()) {
var file = contents.next();
{
attachments.push(file);
}
}
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var cuota = row[3]; // Fourth column
var emailSent = row[4]; // Fifth column
if (emailSent !== EMAIL_SENT && emailAddress !== "#N/A" && cuota !== 0) {
// Prevents sending duplicates, no emails and parents that have already paid
var subject = 'Recordatorio de cuota';
//Do not forget to remove EMAIL_SENT once done otherwise it won't work a 2nd time
var me = Session.getActiveUser().getEmail();
var aliases = GmailApp.getAliases()
GmailApp.sendEmail(emailAddress,subject, message, {attachments: attachments},
{from: aliases[0]};
sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
Try this:
function sendEmails2() {
var ss=SpreadsheetApp.getActive();
var sh=SpreadsheetApp.getActiveSheet();
var sr=2;
var numRows=3;
var rg=sh.getRange(sr,1,sh.getLastRow()-sr+1,sh.getLastColumn());
var contents=DriveApp.getFolderById("*****FolderId******").getFiles();
var attachments=[];
while (contents.hasNext()) {
var file=contents.next();
attachments.push(file);
}
var data=rg.getValues();
for (var i=0; i < data.length; ++i) {
var row=data[i];
var emailAddress=row[0];
var message=row[1];
var cuota=row[3];
var emailSent=row[4];
if (emailSent!="EMAIL_SENT" && emailAddress!="#N/A" && cuota!=0) {
var subject='Recordatorio de cuota';
var me=Session.getActiveUser().getEmail();
var aliases=GmailApp.getAliases()
GmailApp.sendEmail(emailAddress,subject, message,{attachments:attachments,from:aliases[0]});
sh.getRange(sr+i,5).setValue("EMAIL_SENT");
}
}
}
I've been trying to get this one to work without success so far.
I need to get the TaskNumber on the first column of the row I'm on and bring it to the destination sheet, so that I can update it there.
I have the following, which I'm tweaking to achieve my goal, but I guess I've bumped into my limitation walls:
function jump() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var TaskNumberSheet = ss.getSheetByName('To-Do List');
var TaskNoRow = TaskNumberSheet.getActiveCell();
var TaskNoCol = TaskNoRow.getColumn() == 1
var TaskNo = TaskNoCol.getValue;
var sheet = ss.getSheetByName('Updates');
//var Tasks = ss.getSheetByName("To Do List").getActiveRange(Tasks.getColum(1)).getValue();
var values = sheet.getRange("B:B").getValues();
var maxIndex = values.reduce(function(maxIndex, row, index) {
return row[0] === "" ? maxIndex : index;
}, 0);
sheet.setActiveRange(sheet.getRange(maxIndex + 2,2)).setValue(TaskNo);
}
Any help is appreciate.
Cheers,
A
If I understood you correctly, you want to:
Get the value in column A from the currently active row (in sheet To-Do List).
Find the first empty cell in column B (in sheet Updates) (start looking at row #8).
Copy the value that was retrieved in step 1 to the cell retrieved in step 2.
Set the cell retrieved in step 2 as the active cell.
If that's the case, you can do the following:
function jump() {
var ss = SpreadsheetApp.getActive();
// Step 1:
var TaskNumberSheet = ss.getSheetByName('To-Do List');
var TaskNoRow = TaskNumberSheet.getActiveCell().getRow();
var TaskNoCol = 1;
var TaskNo = TaskNumberSheet.getRange(TaskNoRow, TaskNoCol).getValue();
// Step 2:
var sheet = ss.getSheetByName('Updates');
var firstRow = 8;
var column = 2;
var numRows = sheet.getLastRow() - firstRow + 1;
var values = sheet.getRange(firstRow, column, numRows).getValues().map(function(value) {
return value[0]
});
var i = 0;
for (i; i < values.length; i++) {
if (values[i] === "") break;
}
var targetRange = sheet.getRange(i + firstRow, column);
targetRange.setValue(TaskNo); // Step 3
sheet.setActiveRange(targetRange); // Step 4
}
function jump() {
var TargetRow=?;//Fillin target row
var TargetCol=?;//Fillin target column
var ss=SpreadsheetApp.getActive();
var TaskNumberSheet=ss.getSheetByName('To-Do List');
var TaskNoRow=TaskNumberSheet.getActiveCell().getRow();//Getting row from active cell
var TaskNoCol=1
var TaskNo=TaskNumberSheet.getRange(TaskNoRow,TaskNoCol).getValue();
ss.getSheetByName('Updates').getRange(targetRow,targetCol).setValue(TaskNo);
}
I have a spreadsheet with multiple tabs all with a date in column A.
I need a script that I can run using a trigger each night to move all rows with a historic date in Column A to a single tab for historic rows.
I have been successful in getting historic rows in my first sheet to move to the historic tab however I cannot seem to make the script work for multiple tabs.
function HistoricDates() {
SHEET_NAME = "Area1" || "Area2" || "Area3"||"Area4";
// Initialising
var ss = SpreadsheetApp.getActiveSpreadsheet();
var Sheet = ss.getSheetByName(SHEET_NAME);
var PastSheet = ss.getSheetByName("Historic Sheet");
var lastColumn = Sheet.getLastColumn();
// Check all values from sheets
for(var i = Sheet.getLastRow(); i > 0; i--){
// Check if the value is a valid date
var dateCell = Sheet.getRange(i, 1).getValue(); //Dates in column 1
if(isValidDate(dateCell)){
var today = new Date();
var test = new Date(dateCell);
// If the value is a valid date and is a past date, we remove it from the sheet to paste on the other sheet
if(test < today){
var rangeToMove = Sheet.getRange(i, 1, 1, Sheet.getLastColumn()).getValues();
PastSheet.getRange(PastSheet.getLastRow() + 1, 1, 1, Sheet.getLastColumn()).setValues(rangeToMove);
Sheet.deleteRow(i);
}
}
}}
// Check is a valid date
function isValidDate(value) {
var dateWrapper = new Date(value);
return !isNaN(dateWrapper.getDate());
}
The expected result would be for all historic rows in Area 2,3 & 4 to move to move to the single historic tab.
My spreadsheet with script is available on the following link:
https://docs.google.com/spreadsheets/d/1WiZWok4onddTErdAxlWmU82KRSGfVJr5wi1p-rlbY5E/edit?usp=sharing
The way you defined SHEET_NAME, it will always be "Area 1". You can test this.
function test() {
SHEET_NAME = "Area1" || "Area2" || "Area3"||"Area4";
Logger.log(SHEET_NAME);
}
Instead, SHEET_NAME should be an array, and then you need to loop through that array. Below I've included a sample for how to define that array and get the sheets.
function loopThroughSpecificSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNames = ["Area1", "Area2", "Area3", "Area4"];
for (var j in sheetNames) {
var sheet = ss.getSheetByName(sheetNames[j]);
// Do other stuff
}
}
Assuming that the rest of your code works correctly (I haven't analyzed it), your modified script would look like this:
function HistoricDates() {
// Initialising
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pastSheet = ss.getSheetByName("Historic Sheet");
var sheetNames = ["Area1", "Area2", "Area3", "Area4"];
for (var j in sheetNames) {
var sheet = ss.getSheetByName(sheetNames[j]);
var lastColumn = sheet.getLastColumn();
// Check all values from sheets
for(var i = sheet.getLastRow(); i > 0; i--) {
// Check if the value is a valid date
var dateCell = sheet.getRange(i, 1).getValue(); //Dates in column 1
if(isValidDate(dateCell)){
var today = new Date();
var test = new Date(dateCell);
// If the value is a valid date and is a past date, we remove it from the sheet to paste on the other sheet
if(test < today) {
var rangeToMove = sheet.getRange(i, 1, 1, sheet.getLastColumn()).getValues();
pastSheet.getRange(pastSheet.getLastRow() + 1, 1, 1, sheet.getLastColumn()).setValues(rangeToMove);
sheet.deleteRow(i);
}
}
}
}
}
// Check is a valid date
function isValidDate(value) {
var dateWrapper = new Date(value);
return !isNaN(dateWrapper.getDate());
}