I'm using ActiveXObject("Excel.Application") to export html table to Exel in IE Explorer,here is my code:
var curTbl = document.getElementById(tableId);
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var xlsheet = oWB.Worksheets(1);
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
sel.execCommand("Copy");
xlsheet.Paste();
var fname = oXL.Application.GetSaveAsFilename(name+".xls", "Excel Spreadsheets (*.xls), *.xls");
oWB.SaveAs(fname);
oWB.Close(savechanges = false);
oXL.Quit();
oXL = null;
I searched this and get this.
xlsheet.ActiveSheet.Columns(startcol+":"+endcol).ColumnWidth = 22;
But it didn't work and 'ActiveSheet' is undifined.Help ,thank you.
Finally,i find an answer.Use
xlsheet.columns.AutoFit();
or
var myRange = xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(2, 7));
myRange.Columns.AutoFit();
myRange.Rows.AutoFit();
Related
I have a big query that I am using in google scripts and I can't make it past the following as I keep getting the error.
TypeError: Cannot read property 'makeCopy' of undefined
createReport # Report Automation - Sent via Slack.gs:13
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Attribution Extract");
var lr = sheet.getLastRow();
var lc = sheet.getLastColumn();
var sourceData = sheet.getRange(1, 1, lr, lc).getValues();
var timestamp = Utilities.formatDate(new Date(), "America/New_York", "M/d");
for (row in sourceData) {
if (sourceData[row][12] === "RunNow" && sourceData[row][7] !== "")
var templateId = DriveApp.getFileById("1CLzYIh6UqNUrJW5lz9DENoiopOqhQpVLd-XpZ9otzg0");
var copy = templateId.makeCopy();
var newId = copy.getId()
var newSheet = SpreadsheetApp.openById(newId);
var rowNum = parseInt(row);
var opp = sourceData[row][7];
var AMslackID = sourceData[row][14];```
I want to copy srange data in drange on check box validation. below only print data in logger.log but not in drange.
function insertrow(){
var spreadsheet = SpreadsheetApp.getActive();
var full = spreadsheet.getSheetByName("Source");
var shed = spreadsheet.getSheetByName("Destination");
var lr = full.getLastRow();
var srange = full.getRange(2,2,lr,2).getValues();
var dlr = shed.getLastRow();
var drange = shed.getRange(dlr+1,1);
srange.forEach (function(row){
if(row[0]){
Logger.log((JSON.stringify(row)));
}
})
}
Try
function insertrow() {
var spreadsheet = SpreadsheetApp.getActive();
var full = spreadsheet.getSheetByName("Source");
var shed = spreadsheet.getSheetByName("Destination");
var lr = full.getLastRow();
var srange = full.getRange(2, 1, lr, 3).getValues().filter(r => r[0]==true);
var dlr = shed.getLastRow();
shed.getRange(dlr + 1, 1,srange.length,3).setValues(srange);
}
Hi sorry this is probably a very newbie question, I am teaching myself and can't figure out how to google the answer to this one.
I have a loop in google sheets that goes through each field and updates some cells which pull info from a website that I then need to copy across into each row of the loop. It seems to be working through the loop okay however when trying to copy i cant get it into the correct row.
Im sure its something very simple hoping someone out there can point me in the right direction. I'm pretty sure its to do with the code at line 34 but can't figure out how to correct it.
function myLoop() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var Lsheet = ss.setActiveSheet(ss.getSheetByName('Loop'), true);
var rangeData = Lsheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var dataRange = Lsheet.getRange(2,1, lastRow-1, lastColumn-1);
var data = dataRange.getValues();
var len = data.length;
var i = 0
for (i; i < len; i++) {
var row = data[i];
var player = row[4]
var prop = row[6]
var number = row[7]
var overodds = row[8]
var underodds = row[9]
var overreq = row[10]
var underreq = row[11];
if(prop = "Points"){
ss.setActiveSheet(ss.getSheetByName('Stats'), true);
ss.getRange('B1').activate();
ss.getRange("B1").setValue(player);
ss.getRange('AB5').activate();
ss.setActiveSheet(ss.getSheetByName('Loop'), true);
ss.getRange(overreq).activate();
ss.getRange('Stats!AB5').copyTo(ss.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
ss.setActiveSheet(ss.getSheetByName('Stats'), true);
ss.getRange('AB6').activate();
ss.setActiveSheet(ss.getSheetByName('Loop'), true);
ss.getRange(underreq).activate();
ss.getRange('Stats!AB6').copyTo(ss.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
}
}
Modifications of the original code:
Corrected an error in line 24. In your original post, you are using = (assignment operator) to compare your variables. However, you want to use == (equality operator).
Added missing semicolons. Although not necessary, makes for good, consistent style and may prevent some errors.
Moved i declaration inside the for-loop. Makes for cleaner, safer code.
Modified for-loop body so that it uses ranges and setValue(), getValue() methods to copy data.
function myLoop() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var Lsheet = ss.setActiveSheet(ss.getSheetByName('Loop'), true);
var rangeData = Lsheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var dataRange = Lsheet.getRange(2,1, lastRow-1, lastColumn-1);
var data = dataRange.getValues();
var len = data.length;
var statsSheet = ss.getSheetByName('Stats');
var loopSheet = ss.getSheetByName('Loop');
for (var i = 0; i < len; i++) {
var row = data[i];
var player = row[4];
var prop = row[6];
var number = row[7];
var overodds = row[8];
var underodds = row[9];
var overreq = row[10];
var underreq = row[11];
if (prop == "Points") {
statsSheet.getRange('B1').setValue(player);
loopSheet.getRange(overreq).setValues(
statsSheet.getRange('AB5').getValues()
);
loopSheet.getRange(underreq).setValues(
statsSheet.getRange('AB6').getValues()
);
}
}
}
Could you try this:
var currentRow = ss.getActiveRange().getRow();
var currentCol = ss.getActiveRange().getColumn();
.
.
.
ss.getRange('State!AB5').copyTo(ss.getRange(currentRow+1, currentCol),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
I have a value in my sheet where I want to pass the value to other sheet using Google App Script, I have a simple code that can get the value but I don't know on how I can pass it to other sheet and create a new row in other sheet.
function doit() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var FromSheet = ss.getSheetByName('s1');
var ToSheet = ss.getSheetByName('s2');
var firstname = fromsheet.getRange('A2');
var lastname = fromsheet.getRange('B2');
var data = firstname.getValue();
var data1 = lastname.getValue();
//To sheet code???
}
You can simplify a bit the code and it i almost same code to paste value.
function doit() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var FromSheet = ss.getSheetByName('s1');
var ToSheet = ss.getSheetByName('s2');
var firstname = fromsheet.getRange('A2').getValue();
var lastname = fromsheet.getRange('B2').getValue();
//Copy to others sheet
ToSheet.getRange('A2').setValue(firstname);
ToSheet.getRange('A2').setValue(firstname);
}
You can also copy the data with copyValuesToRange()
see : https://developers.google.com/apps-script/reference/spreadsheet/range#copyvaluestorangegridid-column-columnend-row-rowend
code example from site :
var ss = SpreadsheetApp.getActiveSpreadsheet();
var FromSheet = ss.getSheetByName('s1');
var ToSheet = ss.getSheetByName('s2');
var range = FromSheet.getRange("A2:B2");
range.copyTo(ToSheet.getRange("A2"), {contentsOnly:true});
Stéphane
Your code has a couple errors in it:
function doit() {
var ss = SpreadsheetApp.getActiveSpreadsheet() // missing ;
var FromSheet = ss.getSheetByName('s1');
var ToSheet = ss.getSheetByName('s2');
var firstname = fromsheet.getRange('A2'); // fromsheet must be FromSheet (Caps Matter)
var lastname = fromsheet.getRange('B2'); // same here
var data = firstname.getValue();
var data1 = lastname.getValue();
//To sheet code???
}
Here's what I would do:
function doit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var FromSheet = ss.getSheetByName('s1');
var ToSheet = ss.getSheetByName('s2');
var firstname = FromSheet.getRange('A2').getValue(); // simplify lines
var lastname = FromSheet.getRange('B2').getValue(); // simplify lines
ToSheet.getRange("CELL TO PUT FIRSTNAME").setValue(firstname);
ToSheet.getRange("CELL TO PUT LASTNAME").setValue(lastname);
}
My script collects data from a Google Form. And puts it into a Spreadsheet. Based on the info in the spreadsheet it should add Calendar events. When the user chose 1 of 2 countrys in the form i would like the script to execute the function for that country.
Norway is one value
Sweden is one value
For example if the value in var country2 = country1.getValue(); is "Sweden" i would like it to only execute the function sweButik(){ Is it possible, and how do i do it?
function getData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var lastCell = sheet.getRange(lastRow, lastColumn);
var time1 = sheet.getRange(lastRow, 3);
var name1 = sheet.getRange(lastRow, 4);
var info1 = sheet.getRange(lastRow, 5);
var country1 = sheet.getRange(lastRow, 6);
var time2 = time1.getValue();
var name2 = name1.getValue();
var info2 = info1.getValue();
var country2 = country1.getValue();
}
//////////////////////////
function sweButik(){
var sweButik1 = CalendarApp.getCalendarById('company.com_7gvpuutc6h1d7kk5mjb2s5f4ds#group.calendar.google.com').createEvent(name2,
new Date(time2),
new Date(time2), {description: info2});
var sweButik2 = CalendarApp.getCalendarById('company.com_bertj0lfja45425ran8mmrpgc8#group.calendar.google.com').createEvent(name2,
new Date(time2),
new Date(time2), {description: info2});
}
/////////////////////////
function norButik(){
var norButik1 = CalendarApp.getCalendarById('company.com_7d88i0ne013r26i172k8fu16j0#group.calendar.google.com').createEvent(name2,
new Date(tid2),
new Date(tid2), {description: info2});
var norButik2 = CalendarApp.getCalendarById('company.com_sb0ddqmj5ql6uu5769u0qfs2go#group.calendar.google.com').createEvent(name2,
new Date(time2),
new Date(time2), {description: info2});
}
Since your values are fixed, you can do something like :
var country2 = country1.getValue();
if (country2 == "Sweden") {
sweButik();
} else if (country2 == "Norway") {
norButik();
}
But you need some parameters to be passed to these functions, so sweButik will become :
function sweButik (name,time,info) {
var sweButik1 = CalendarApp.getCalendarById('company.com_7gvpuutc6h1d7kk5mjb2s5f4ds#group.calendar.google.com').createEvent(name,
new Date(time),
new Date(time), {description: info});
var sweButik2 = CalendarApp.getCalendarById('company.com_bertj0lfja45425ran8mmrpgc8#group.calendar.google.com').createEvent(name,
new Date(time),
new Date(time), {description: info});
}
And its call will be :
sweButik(name2, time2, info2);
Don't forget to do the same for norButik