Reputation: 13
I have a small script which add new column. Unfortunately it add a column in wrong place and without header. What I need to add to get a column with increasing number in header?
At the moment I have:
function doGet(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
sh.insertColumns(1);
}
Upvotes: 0
Views: 1421
Reputation: 193
This code adds a column after the last column that contains a value. The header becomes Obciązenie [num] tyg.
per your screenshot example.
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
var col = sh.getLastColumn();
var colContents = sh.getRange(1, col).getValue();
var previousHeaderNum = colContents.toString().replace(/\D/g, '');
var headerNum = (parseInt(previousHeaderNum, 10) +1);
var header = "Obciązenie " + headerNum + " tyg."
sh.insertColumnAfter(col);
sh.getRange(1, col+1).setValue(header);
}
Upvotes: 2