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);
}
Related
Hello so I am trying to get values from the 'book' sheet then copy it to 'Completed' sheet. It takes the values form book stores it in the data array and copy them to the 'Completed' sheet. Its working fine but very slow which jeopardizes my work in time-wise. How can I make this run faster ?
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var clear_sheet = spreadsheet.getSheetByName('Completed_Orders'); // clear the destination sheet first
clear_sheet.getRange('A2:X').clear();
var sheet = spreadsheet.getSheetByName('book'); //source sheet
var Datarange = sheet.getRange('Q3:Q'); //range to check
var Datavalue = (Datarange.getValues());
var dest = spreadsheet.getSheetByName('Completed_Orders'); //destination sheet
var data = [];
for (i=0; i<Datavalue.length;i++) {
if ( Datavalue[i] == "Completed") {
data.push.apply(data,sheet.getRange(i+3,1,1,24).getValues());
}
}
dest.getRange(2,1,data.length,data[0].length).setValues(data);
var column = dest.getRange('A3:A');
var values = column.getValues(); // get all data in one call
var ct = 0;
while ( values[ct][0] != "" ) { // to find the last row correctly, getLastRow is not working perfectly
ct++;
}
var endRow = ct+2;
}
Try this:
Don't use this syntax var values=sheet.getRange('A3:A').getValues() because it get's the data all the way down to getMaxRows(). Instead use var values=sheet.getRange(3,1,sheet.getLastRow()-2,1).getValues().
Also Datavalue[i] is a whole row
function myfunction() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var clear_sheet = spreadsheet.getSheetByName('Completed_Orders');
clear_sheet.getRange(2,1,clear_sheet.getLastRow()-1,24).clear();
var sheet = spreadsheet.getSheetByName('book'); //source sheet
var Datarange = sheet.getRange(3,17,sheet.getLastRow()-2,1); //range to check
var Datavalue = Datarange.getValues();
var dest = spreadsheet.getSheetByName('Completed_Orders'); //destination sheet
var data = [];
for (var i=0;i<Datavalue.length;i++) {
if (Datavalue[i]["************You need another index here**************"] == "Completed") { //need another index Datavalue is 2d
data.push(sheet.getRange(i+3,1,1,24).getValues());
}
}
dest.getRange(2,1,data.length,data[0].length).setValues(data);
}
I think this version will be a lot faster.
function myfunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var ssh=ss.getSheetByName('book');
var dsh=ss.getSheetByName('Completed_Orders');
dsh.getRange(2,1,dsh.getLastRow()-1,24).clear();
var dv=ssh.getRange(3,1,ssh.getLastRow()-2,24).getValues();//this version just gets this data one time so it should a lot faster.
var data=[];
for (var i=0;i<dv.length;i++) {
if (dv[i][16]=="Completed") {
data.push(dv[i]);
}
}
dsh.getRange(2,1,data.length,data[0].length).setValues(data);
}
I'm new to Google Apps Scripts so I am sorry if my question is redundant.
I am working on a variant of the scripts here:
Google Sheets: delete rows containing specified data
(Here is the script that I have been editing -- note the empty IF value.)
function onOpen(){
}
function deleteFunction(){
var sheetName = "Title";
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName(sheetName);
var dataRange = sheet.getDataRange();
var numRows = dataRange.getNumRows();
var values = dataRange.getValues();
var rowsDeleted = 0;
for (var i = 2; i <= numRows; i++){
var rowValues = values[i-1].toString();
if (rowValues == ""){
sheet.deleteRow(i - rowsDeleted);
rowsDeleted++;
}
I would like to delete rows in a sheet named "Title" that do not contain "| My Text Here". The text is found in a string of text, such as "Here is the string of text that could be random | My Text Here". So if "| My Text Here" is not found in a cell, I want that whole row to be deleted.
The data set I want this to work with will have ~10,000 rows and I want this script to either run when the sheet is opened or once a day.
I have tried to make this work, but I think I'm on the wrong track and so would really apprecaite the communities help!
I can attach a test sheet if needed.
Thank you in advance for your help and guidance
New script:
function main() {
var SS = SpreadsheetApp.getActive();
var SHEET = SS.getSheetByName("Title");
var RANGE = SHEET.getDataRange();
var DELETE_VAL = "| TEST TEXT HERE";
var COL_TO_SEARCH = 0; //Zero is first
var deleteSelectedRows = removeThenSetNewVals();
};
function removeThenSetNewVals(){
var SS = SpreadsheetApp.getActive();
var SHEET = SS.getSheetByName("Title");
var RANGE = SHEET.getDataRange();
var rangeVals = RANGE.getValues();
var DELETE_VAL = "| TEST TEXT HERE";
var COL_TO_SEARCH = 0; //Zero is first
var newRangeVals = [];
for(var i = 0; i < rangeVals.length; i++){
if(rangeVals[i][COL_TO_SEARCH] == DELETE_VAL){
newRangeVals.push(rangeVals[i]);
};
};
RANGE.clearContent();
var newRange =
SHEET.getRange(1,1,newRangeVals.length, newRangeVals[0].length);
newRange.setValues(newRangeVals);
};
}
Your code needs the following modification to work in the way you desire
Use indexOf(). This is a Javascript method that allows you to verify either a sub-string is contained in a string. In your case, it allows you to verify either "| My Text Here" is contained in your cells in column A.
Loop "backwards" from the end row to the start row. Why? Because otherwise the deletion of rows will mess up the row indices of the remaining rows.
function deleteFunction(){
//declarations
var sheetName = "Title";
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName(sheetName);
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
for(var i=values.length-1;i>0;i--){
var myValue=values[i][0];
Logger.log(i);
if( myValue.indexOf("| My Text Here")==-1){
Logger.log(myValue);
sheet.deleteRow(i+1);
}
}
}
-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);
}
}
}
I've played around with this script but have still yet to get it to work.
I need a script that looks at the column F's background color in the sheet called Test.
If the cell background in Column F is yellow it will copy that entire row to a new sheet called New Data.
Please find attached the Google Spreadsheet link: https://docs.google.com/spreadsheets/d/19pMAmB94tOoV14MiSPyoPhty2dO5OudO4NupP8KhJA4/edit#gid=1703438513
Any help would be greatly appreciated!
function Extract(){
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Test');
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getBackgrounds();
var yellowColour = '#ffff00';
var whiteColour = '#ffffff';
var newSheet = ss.getSheetByName('New Sheet');
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[6] === whiteColour ) {
}
else {
newSheet.appendRow(row);
}
} // End of For Loop
};
Try this. It creates an array of rows where F is yellow. It clears the old data in 'New Data' and copies in the new array:
function Extract(){
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Test');
var lr=sheet.getLastRow()
var data = sheet.getRange(2,1,lr,15).getValues();//get Test values
var rows = sheet.getRange(2,6,lr,1).getBackgrounds();//get F background colors
var yellowColour = '#ffff00';
var newSheet = ss.getSheetByName('New Data');
var lr1= newSheet.getLastRow()
var newData=[] //new array
for (var i = 0; i <= lr - 1; i++) {
if (rows[i] == yellowColour ) { //if F = yellow
newData.push(data[i]) // add to new array
}}
newSheet.getRange(2,1,lr1+1,15).clearContent()//clear old content excluding header row
newSheet.getRange(2,1,newData.length,15).setValues(newData)//set new comtent
};
I have a Google form set up which puts the responses into a Google Spreadsheet, I have added a few extra columns to the sheet to help us monitor the status of each response. Is there a way I can set the default value for a column each time a new row is created? (i.e. each time a new response is submitted). Below is the code I have so far, but currently it only does it for one row.
function setDefault() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastRow = ss.getLastRow();
for (var i = 3; i <= lastRow; i++)
{
sheet.getRange('A' + i).activate();
var value = sheet.getActiveCell().getValue();
while (value != "")
{
sheet.getActiveCell().offset(0, 5).activate();
sheet.getActiveCell().setValue("Unknown");
sheet.getActiveCell().offset(0, 1).activate();
sheet.getActiveCell().setValue("New");
}
}
};
I solved it using the following code and set the project trigger to "On form submitted" so that it runs each time a new response is submitted.
function setDefault() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastRow = ss.getLastRow();
for (var i = 3; i <= lastRow; i++)
{
sheet.getRange('A' + i).activate();
sheet.getActiveCell().offset(0, 5).activate();
sheet.getActiveCell().setValue("Unknown");
sheet.getActiveCell().offset(0, 1).activate();
sheet.getActiveCell().setValue("New");
}
};
The method through last row might have troubles that the form submission isn't always the last row if you manually edit the spread sheet or if a user edit his response! That happens a the time ><"
I suggest another approach, try this:
function onFormSubmit(e){
var r = e.range.rowStart;
Logger.log(r);
}
}
Actually you can just log out the variable "e" to find out more submission informations ##b