Clear Data rather then deleting row - copy

I have the script below that works for what i need except the ending where it deletes the row out. What I am in need of is it to copy and paste from Sheet1 to Sheet2, and clear the row out of sheet1 except for column A.
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1'); //source sheet
var testrange = sheet.getRange('D:D');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('Sheet2'); //destination sheet
var data = [];
var j =[];
//Condition check in D:D; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'x') {
data.push.apply(data,sheet.getRange(i+1,1,1,11).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).
setValues(data);
//Delete matched rows in source sheet ** I Need these to clear data
from B:E for row that it copied and pasted rather then deleted it out.***
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k); // I have tried sheet.Clearcontents, with no sucess
//Alter j to account for deleted rows
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
}

I was able to find an alternate way of clearing what i need which was thru another script that clear any orange cell. I used conditional formatting to get this to work. See below. Thanks....
function copyandclearorange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Research'); //source sheet
var testrange = sheet.getRange('L:L');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('Log'); //destination sheet
var data = [];
var j =[];
//Condition check in L:L; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'x') {
data.push.apply(data,sheet.getRange(i+1,1,1,11).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
var sheet = SpreadsheetApp.getActive().getSheetByName('Research');
var range = sheet.getDataRange();
var bgColors = range.getBackgrounds();
for (var i=0; i<bgColors.length; i++) {
for (var j=0; j<bgColors[i].length; j++) {
if (bgColors[i][j] === '#ff9900') {
range.getCell(i+1,j+1).clearContent();
}
}
}
}

Related

Google Script copy and paste as values debug

Looking to
Find cell in first row that matches yesterday's date (this part works)
Select column that cell belongs to
Copy entire column and paste as values
Not sure where the code is breaking
function CopyandPasteasValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1];
// sheet is the first worksheet in the spreadsheet
var today = new Date();
var yesterday = new Date();
yesterday.setDate(today.getDate()-1);
yesterday.setHours(3,0,0,0); //comparison doesn't seem to work without this
for(var i=0; i< 26; i++) {
var cell = sheet.getRange(1,i+2); //start getting sheet date at cell C1
var sheetdate = cell.getValue();
if(sheetdate.valueOf() == yesterday.valueOf()) {
// if there's a match, set the col
var col = (i);
// copy column and paste as values -- BELOW DOESN"T WORK --
function copyandpastescol() {
sheet.getRange(1, col+2, sheet.getMaxRows(), 1).activate();
sheet.getActiveRange().copyTo(sheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
}
}
}
Duh... didn't need the extra function! Thanks...
/** #OnlyCurrentDoc */
function CopyandPasteasValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// ss is now the spreadsheet the script is associated with
var sheet = ss.getSheets()[1]; // sheets are counted starting from 0
// sheet is the first worksheet in the spreadsheet
// set and store a date object for today
var today = new Date();
var yesterday = new Date();
yesterday.setDate(today.getDate()-1);
yesterday.setHours(3,0,0,0);
// iterate the values in the range object
for(var i=0; i< 26; i++) {
var cell = sheet.getRange(1,i+2); //start getting sheet date at cell C1
var sheetdate = cell.getValue();
// Compare only values of the objects
if(sheetdate.valueOf() == yesterday.valueOf()) {
// if there's a match, set the col
var col = (i+2);
// copy column and paste as values
sheet.getRange(1, col, sheet.getMaxRows(), 1).activate();
sheet.getActiveRange().copyTo(sheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
}
}

Script for Date Stamping on Multiple Sheets

I am very very very new to all this. I need help, I am trying to use script editor to get the date statically stamped in one column when something is entered in a different column. I figured how to do this for one tab but I need this to happen on multiple tabs in the same sheet and I'm struggling to get it to work. Is there one code that will work for this? This is the script I was using for one tab:
/**
* Creates a Date Stamp if a column is edited.
*/
//CORE VARIABLES
// The column you want to check if something is entered.
var COLUMNTOCHECK = 9;
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION = [0,-8];
// Sheet you are working on
var SHEETNAME = 'Sheet 2'
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//checks that we're on the correct sheet.
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
//checks the column to ensure it is on the one we want to cause the date to appear.
if( selectedCell.getColumn() == COLUMNTOCHECK) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}
Thank you for your time in advance.
To proceed with the function on multiple sheets, you can check for the sheet name in an array of acceptable names.
function onEdit() {
var colToCheck = 9;
// Offset from the input [row, column]
var dateOffset = [0, -8];
// Sheets to proceed on
var sheetNames = ['Sheet 2', 'Sheet 3'];
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var name = sheet.getName();
if (sheetNames.indexOf(name) > -1) {
var cell = sheet.getActiveCell();
var col = cell.getColumn();
if (col == colToCheck) {
var dateTimeCell = cell.offset(dateOffset[0], dateOffset[1]);
dateTimeCell.setValue(new Date());
}
}
}
References
Arrays
indexOf()
EDIT ONE
If you want multiple options, you could set them up in arrays. The order of the elements in the arrays must match.
This code assumes that the timestamp is always on the same row.
function onEdit() {
var sheetNames = ['Sheet 2', 'Sheet 3'];
var colsToCheck = [9, 15];
var colOffsets = [-8, -4];
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var name = sheet.getSheetName();
var index = sheetNames.indexOf(name);
if (index > -1) {
var cell = sheet.getActiveCell();
var col = cell.getColumn();
if (col == colsToCheck[index]) {
var dateTimeCell = cell.offset(0, colOffsets[index]);
dateTimeCell.setValue(new Date());
}
}
}
EDIT TWO
For those of you who would prefer objects
function onEdit() {
var sheets = {
'Sheet 2': {
checkCol: 9,
offset: -8
},
'Sheet 3': {
checkCol: 15,
offset: -4
}
};
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var name = sheet.getSheetName();
var settings = sheets[name];
if (settings) {
var cell = sheet.getActiveCell();
var col = cell.getColumn();
if (col == settings.checkCol) {
var dateTimeCell = cell.offset(0, settings.offset);
dateTimeCell.setValue(new Date());
}
}
}

getContextByIndex doesn't return all the columns

I have following code
oTableEntry = this.getView().byId("oTable");
var count = oTableEntry._getRowCount();
var oTData;
for (var i = 0; i < count; i++) {
oTData = oTableEntry.getContextByIndex(i).getObject();
oTData doesn't contain values for all the columns even though they appear in the table. Am I doing something wrong here?
I think you are trying to get the table columns and their values for each row. Here is how you can do this:
var oTable = this.getView().byId("oTable");//Get the table by Id
var aItems = oTable.getAggregation("items");//get the items of the table
for (var i=0; i<aItems.length; i++) {
var tableColumns = aItems[i].getAggregation("cells"); //Here tableColumns will //have all the columns of the table rows.
var column1Value = tableColumns[1].getProperty("text");
}
Hope you are looking for same solution!!
var oTable = sap.ui.getCore().byId("YourTableID");
if (oTable) {
var oColumns = oTable.getColumns();//get all columnms
var oRows = oTable.getItems();
for (var r in oRows) {
var oRow = oRows[r];
console.log(oRow.getBindingContext().getObject());//return the row data
}
}

how to get 1 item from a data binding in the controller?

I have a table that is filled with data via data binding. And now in my controller i'm trying to loop over all items of the databinding. This is what i have so far but i can't get it to work.
colorRows : function(oTable) {
var items = oTable.getBinding("items");
var rowCount = items.length; //number of visible rows
var currentRowContext;
for (var i = 0; i < rowCount; i++) {
currentRowContext = items[i].getValue(); //this won't work
}
}
so i need to get a value from the item with the index that matches i.
edit: i'm using sap.m.table
As Keshet mentioned, it depends on the Table you are using. Here is an exapmle for the sap.ui.table.Table. First you get the Context of each Row and then you can access the Data that is saved on the Row (btw, there is no such thing as RowValue):
colorRows: function(oTable) {
var aRows = oTable.getRows();
var currentRowValue;
for (var i = 0; i < aRows.length; i++) {
var oRowContext = oTable.getContextByIndex(i);
if (oRowContext) {
var oRowObject = oRowContext.getObject();
// or you can use the getProperty method
var oSingleValue = oRowContext.getProperty("yourPropertyName");
}
}
}
I assume you use sap.m.Table.
colorRows : function(oTable) {
var sPath = oTable.getBinding("items").getPath(); //path to table's data
var oModel = this.getView().getModel(); //model which is bound to the table
//or var oModel = oTable.getModel(); if the model is bound directly to the table
var aData = oModel.getProperty(sPath);//array of rows
var rowCount = aData.length;
var currentRowContext;
for (var i = 0; i < rowCount; i++) {
currentRowContext = aData[i];
}
}
Here is a working example.

get values from XML file in my Titanium Project

I am having the following .xml which i used in my android project.now,am trying to create the same in iPhone using titanium.Can some one tell me how to get this values from the xml file and show them in a table view.? if i give MainCategories the 3 values in it should appear in the table view.Also can some one tell me is there any other better option to achieve this?or can we use plist??thank you.
<string-array name="MainCategories">
<item>Acceleration</item>
<item>Angle</item>
<item>Area</item>
</string-array>
<string-array name="Acceleration_array">
<item>meter/sq sec</item>
<item>km/sq sec</item>
<item>mile/sq sec</item>
<item>yard/sq sec</item>
</string-array>
<string-array name="Angle_array">
<item>degree</item>
<item>radian</item>
<item>grad</item>
<item>gon</item>
</string-array>
Try This.
var result = [];
var fileName = 'arrays1.xml'; //save xml file
var tableView = Ti.UI.createTableView({ data : result, width : '100%', height : '100%' });
function readXML(fileName)
{
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, fileName);
var xmltext = file.read().text;
var doc = Ti.XML.parseString(xmltext);
var parentNodeLength = doc.documentElement.getElementsByTagName('string-array').length;
for (var i = 0; i < parentNodeLength; i++) {
var attrValue = doc.documentElement.getElementsByTagName('string-array').item(i).attributes.getNamedItem('name').nodeValue;
if (attrValue === 'Angle_array') {
var parentNode = doc.documentElement.getElementsByTagName('string-array').item(i);
var subNodeLength = parentNode.getElementsByTagName('item').length;
for (var j = 0; j < subNodeLength; j++) {
var title = parentNode.getElementsByTagName('item').item(j).text;
var row = Ti.UI.createTableViewRow({
height : 110
});
var label = Ti.UI.createLabel({
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
text : title
});
row.add(label);
result.push(row);
}
}
}
}
readXML(fileName);
tableView.setData(result);
win1.add(tableView);
win1.open();
i am not familiar with titanium but maybe this should help you out:
var result = this.responseText;
var xml = Ti.XML.parseString(result);
var params = xml.documentElement.getElementsByTagName("member");
var name = xml.documentElement.getElementsByTagName("name");
var value = xml.documentElement.getElementsByTagName("string");
var data = [];
for (var i=0;i<params.item.length;i++) {
Ti.API.log('Param '+i+': Name: '+n.item(i).text);
Ti.API.log('Param '+i+': Value: '+v.item(i).text);
// Add to array
data.push({"name":n.item(i).text,"value":v.item(i).text});
}
copied from this link
for displaying it in uitableview, save your values in nsarray and display your array in tableview. here is a tutorial for UITableView.