function writeData () {
var sss= SpreadsheetApp.getActiveSpreadsheet();
//var inSheet2 = sss.getSheetByName('Copy of 01-Raw Data 2');
var inSheet2 = sss.getSheets()[4];
var input2 = inSheet2.getRange(2, 1, inSheet2.getLastRow(),42);
var outSheet1 = sss.getSheets()[0];
var outStartRow2 = outSheet1.getLastRow() + 2;
var outStartRow3 = outSheet1.getLastRow() + 2;
input2.copyFormatToRange(outSheet1, 2, 42, outStartRow3, outStartRow2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var inSheet = sss.getSheets()[0];
var input = inSheet.getRange(2, 18, inSheet.getLastRow(), 42).getValues();
var outSheet2 = sss.getSheets()[4];
var outStartRow = outSheet2.getLastRow() + 1;
for (var col = 0; col < input.length; col++) {
for (var row = 0; row < input[col].length; row++) {
outSheet2.getRange(outStartRow + row, col + 5)
.setValue(input[col][row]);
}
}
}
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push({name: "Write data", functionName: "writeData"});
ss.addMenu("Custom functions", menuEntries);
}
I am using google-apps-script in Google spreadsheets.
What i have:
What I want to achieve:
Please, let me know if you got a way to make this correctly.
The Code:
function myFunction() {
var ss=SpreadsheetApp.getActive();
var sh0=ss.getSheetByName('INPUT');
var sh1=ss.getSheetByName('OUTPUT');
var rg0=sh0.getDataRange();
var vA=rg0.getValues();
var oA=[];
for(var i=0;i<vA.length;i++)
{
for(var j=0;j<vA[0].length;j++)
{
if(vA[j][i])
{
oA.push([vA[j][i]]);
}
else
{
for(var k=0;k<j;k++)
{
oA.pop();
}
break;
}
}
}
sh1.getRange(1,1,oA.length,oA[0].length).setValues(oA);
}
Data Sheet:
Output Sheet:
Related
I have a code as follows:
function DetailFacture2() {
var ss = SpreadsheetApp.getActive();
var DetailDEVIS = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailDEVIS'));
var FACTUREDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('FACTUREDevis'));
var DetailFactureDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailFactureDevis'));
var lastrowpaste = FACTUREDevis.getLastRow();
var numrow = FACTUREDevis.getRange(lastrowpaste,13).getValue()
var lastrowpaste2 = DetailFactureDevis.getLastRow() - numrow +2;
var data = DetailDEVIS.getDataRange().getValues();
var DetailD = FACTUREDevis.getRange(lastrowpaste,2).getValue();
for(var i = 0; i<data.length;i++){
if(data[i][1] == DetailD){ //[1] because column B
var firstrowcopy = i+1;
Logger.log(firstrowcopy)
return (firstrowcopy)
}
}
};
It does return the correct value, but how do you use "firstrowcopy" as a fixed var?
I would like to use as follows:
function DetailFacture2() {
var ss = SpreadsheetApp.getActive();
var DetailDEVIS = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailDEVIS'));
var FACTUREDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('FACTUREDevis'));
var DetailFactureDevis = SpreadsheetApp.setActiveSheet(ss.getSheetByName('DetailFactureDevis'));
var lastrowpaste = FACTUREDevis.getLastRow();
var numrow = FACTUREDevis.getRange(lastrowpaste,13).getValue()
var lastrowpaste2 = DetailFactureDevis.getLastRow() - numrow +2;
var data = DetailDEVIS.getDataRange().getValues();
var DetailD = FACTUREDevis.getRange(lastrowpaste,2).getValue();
for(var i = 0; i<data.length;i++){
if(data[i][1] == DetailD){ //[1] because column B
var firstrowcopy = i+1;
var source = DetailDEVIS.getRange(firstrowcopy,1,numrow-1);
var destination = DetailFactureDevis.getRange(lastrowpaste2,3);
source.copyTo(destination);
}
}
};
But, as one would expect, it cannot work as it loops...
Not sure if I understand your question too. The code doesn't look well. Here is just my guess. Try to change the last lines this way:
// ...
var firstrowcopy = 0;
for (var i = 0; i < data.length; i++){
if(data[i][1] == DetailD){ //[1] because column B
firstrowcopy = i+1;
break;
}
}
var source = DetailDEVIS.getRange(firstrowcopy,1,numrow-1);
var destination = DetailFactureDevis.getRange(lastrowpaste2,3);
source.copyTo(destination);
}
I was trying to put some information of my sheet in a array, to use in a graphic later. But this error keeps showing :(
Error: Syntax error (line 8, archive "Código")
function onOpen() {
var proposta = SpreadsheetApp.getActive().getSheetByName('Proposta de solução');
var ids = proposta.getRange('A10:A26');
var names = proposta.getRange('B10:B26');
var esforcos = proposta.getRange('F10:F26');
var name = [
for (var i = 0; i < 17; i++) {
names.getCell(i, 1).getValue();
}
]
var id = [
for(var j = 0; j < 17; j++) {
ids.getCell(j,1).getValue();
}
]
var esforco = [
for(var k = 0; k < 17; k++) {
esforcos.getCell(k,1).getValue();
}
]
}
This should get the results you want:
function onOpen() {
var proposta = SpreadsheetApp.getActive().getSheetByName('Proposta de solução');
var ids = proposta.getRange('A10:A26');
var names = proposta.getRange('B10:B26');
var esforcos = proposta.getRange('F10:F26');
var name = [];
var id = [];
var esforco = [];
for (var i = 0; i < 17; i++) {
name.push(names.getCell(i, 1).getValue());
id.push(ids.getCell(i, 1).getValue());
esforco.push(esforcos.getCell(i, 1).getValue());
}
}
I want to concatenate columns that are dynamic, using appscript programmatically.
See the screenshot what my data looks like and what output i am trying to achieve:
I was able to achieve concatenation of static/fixed no. of columns using the below code.
function concatenate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getSheetByName("Sheet1");
var lr = sheetName.getLastRow();
var lc = sheetName.getLastColumn();
var rangeValue = sheetName.getRange(2, 1, lr-1, lc).getValues();
for(var i=0;i<rangeValue.length; i++) {
var setRange = sheetName.getRange(i+2, lc);
setRange.setValue(rangeValue[i][0] + " " + rangeValue[i][1] + " " + rangeValue[i][2])
};
};
All i want to achieve now is, the same above for DYNAMIC columns. I tried my best to figure it out, but in vain. Any help would be much greatly appreciated.
Splicing in another column
This assumes that you start with only your data columns and that you have not added the header for the new column. It can be made to work both ways but you just need to know what your starting with.
function myFunction() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet 1');
var rg=sh.getDataRange();
var vA=rg.getValues();
for(var i=0;i<vA.length;i++){
if(i==0){
vA[i].splice(vA[i].length,0,'Concatenate');
}else{
vA[i].splice(vA[i].length,0,vA[i].join(' '));
}
}
sh.getRange(1,1,vA.length,vA[0].length).setValues(vA);
}
I believe you can solve this by just looping through the columns using the last column index lc variable you already have. Should look something like this:
function concatenate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getSheetByName("Sheet1");
var lr = sheetName.getLastRow();
var lc = sheetName.getLastColumn();
var rangeValue = sheetName.getRange(2, 1, lr-1, lc).getValues();
for(var i = 0; i < rangeValue.length; i++) {
var catValues = "";
for (var j = 0; j < lc; j++) {
catValues += rangeValue[i][j] + " ";
}
var setRange = sheetName.getRange(i+2, lc);
setRange.setValue(catValues);
};
};
Or if you wanted to get fancy with .join():
function concatenate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getSheetByName("Sheet1");
var lr = sheetName.getLastRow();
var lc = sheetName.getLastColumn();
var rangeValue = sheetName.getRange(2, 1, lr-1, lc).getValues();
for(var i = 0; i < rangeValue.length; i++) {
var catValues = sheetName.getRange(i, lc).getValues().join(" ");
var setRange = sheetName.getRange(i+2, lc);
setRange.setValue(catValues);
};
};
I have a problem, I don't know how create button in a Google spreadsheet which will find the last empty row and add in first column next number (n+1) and in second column actual date.
function selectFirstEmptyRow() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.setActiveSelection(sheet.getRange("A" + getFirstEmptyRowWholeRow()))
// copy value from row
var ss = SpreadsheetApp.getActiveSheet();
ss.insertRowAfter(ss.getLastRow());
ss.getRange(1, 1).copyTo(ss.getRange(ss.getLastRow() + 1, 1));
// add 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeRange = ss.getActiveRange();
var cell, cellValue, cellFormula;
// iterate through all cells in the active range
for (var cellRow = 1; cellRow <= activeRange.getHeight(); cellRow++) {
for (var cellColumn = 1; cellColumn <= activeRange.getWidth(); cellColumn++) {
cell = activeRange.getCell(cellRow, cellColumn);
cellFormula = cell.getFormula();
if (cellFormula[0] != "=") {
cellValue = cell.getValue();
cell.setValue(cellValue + 1);
}
}
}
}
function getFirstEmptyRowWholeRow() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
var row = 3;
for (var row = 3; row < values.length; row++) {
if (!values[row].join("")) break;
}
return (row + 1);
}
You can add a custom menu to a Google spreadsheet:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('My menu')
.addItem('Add new date row', 'insertAtNextEmptyRow')
.addToUi();
}
function insertAtNextEmptyRow() {
var firstRow = 3;
var sheet = SpreadsheetApp.getActiveSheet();
var row = getNextEmptyRow(firstRow);
var previousNum = SpreadsheetApp.getActiveSheet().getRange('A' + (row - 1)).getValue()
if (previousNum !== parseInt(previousNum, 10))
previousNum = row - firstRow - 1;
SpreadsheetApp.getActiveSheet().getRange('A' + row).setValue(previousNum + 1);
var date = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
SpreadsheetApp.getActiveSheet().getRange('B' + row).setValue(date);
}
function getNextEmptyRow(firstRow) {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
for (var row = firstRow; row < values.length; row++) {
if (!values[row].join(""))
return (row + 1);
}
return null;
}
Here is what it'll look like:
I have a jqgrid populated by some fields. I want some of the cells to be
editable:true
or
editable:false
based on a condition
Here's my function (EDITED):
var grid = $("#mygrid");
var getColumnIndexByName = function(gr,columnName) {
var cm = gr.jqGrid('getGridParam','colModel');
for (var i=0,l=cm.length; i<l; i++) {
if (cm[i].name===columnName) {
return i; // return the index
}
}
return -1;
};
function abilitaDisabilitaEditRecord() {
var pos=getColumnIndexByName(grid,'descrizione');
var pos2=getColumnIndexByName(grid,'endDate');
var allIds = $('#mygrid').jqGrid('getDataIDs');
var cells = $("tbody > tr.jqgrow > td:nth-child("+(pos+1)+")",grid[0]);
var cells2 = $("tbody > tr.jqgrow > td:nth-child("+(pos2+1)+")",grid[0]);
for (var i = 0; i < allIds.length; i++) {
for (var j=0; j<cells.length; j++) {
var cell = $(cells[j]);
var cell2 = $(cells2[j]);
var checkDataFine = $('#mygrid').jqGrid('getCell', allIds[i], 'date');
if (!checkDataFine==false) {
cell.addClass('not-editable-cell');
cell2.addClass('not-editable-cell');
}
}
}
}
you can call this in you else condition, i didn't test it, but i think this should work
$("#mygrid").jqGrid('restoreRow',allIds[i]);
Here's the working solution:
var grid = $("#mygrid");
var getColumnIndexByName = function(gr,columnName) {
var cm = gr.jqGrid('getGridParam','colModel');
for (var i=0,l=cm.length; i<l; i++) {
if (cm[i].name===columnName) {
return i;
}
}
return -1;
};
function changeEditableByContain() {
var pos=getColumnIndexByName(grid,'field1');
var pos2=getColumnIndexByName(grid,'field2');
var pos3=getColumnIndexByName(grid,'field3');
var pos4=getColumnIndexByName(grid,'field4');
var allIds = $('#mygrid').jqGrid('getDataIDs');
var cells = $("tbody > tr.jqgrow > td:nth-child("+(pos+1)+")",grid[0]);
var cells2 = $("tbody > tr.jqgrow > td:nth-child("+(pos2+1)+")",grid[0]);
var cells3 = $("tbody > tr.jqgrow > td:nth-child("+(pos3+1)+")",grid[0]);
var cells4 = $("tbody > tr.jqgrow > td:nth-child("+(pos4+1)+")",grid[0]);
for (var i = 0; i < allIds.length; i++) {
var cell = $(cells[i]);
var cell2 = $(cells2[i]);
var cell3 = $(cells3[i]);
var cell4 = $(cells4[i]);
if (condition...) {
cell.addClass('editable-cell');
cell2.addClass('editable-cell');
cell3.addClass('editable-cell');
cell4.addClass('editable-cell');
}
else{
cell.addClass('not-editable-cell');
cell2.addClass('not-editable-cell');
cell3.addClass('not-editable-cell');
cell4.addClass('not-editable-cell');
}
}
}
and then call onloadcomplete:
changeEditableByContain();