Script eror message - javascript

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];```

Related

i want to copy data from one sheet another sheet only select rows with checkbox In Google App Script

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);
}

Trying to sort and remove few columns before email it as an attachment using App Scripts but shows errors?

I am sending a Google sheet Tab to students and it is working fine. But before sending it to them, I need to sort the sheet using Column3 value and then remove column2, column4, column5, column7. But it shows errors:
"TypeError: The comparison function must be either a function or
undefined" at contents.sort() and contents.delete() in the codes
below.
I reviewed many posts but can't figure out the issue.
Function sendEmails(){
var sss = SpreadsheetApp.getActiveSpreadsheet();
var ssID = sss.getId();
var sheetName = sss.getName();
var sh = SpreadsheetApp.getActive();
var sheet1 = sh.getSheetByName("TempDataSet");
var shID = sheet1.getSheetId().toString();
var subject = 'Your Attendance Record at BDU';
var body = 'Dear Student,'+ '\n\n' + 'Greetings! Please find the attendance record attached for your reference.' + '\n\n' + 'Thank you.';
var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=xlsx&id="+ssID+"&gid="+shID;
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
var column = 3;
contents.sort({column: column, ascending:true});
contents.delete({column: column2, column4, column5, column7});
var sheet2 = sh.getSheetByName('StudentList');
var data = sheet2.getLastRow();
var students = [];
var students = sheet2.getRange(2, 6, data).getValues();
for (var i=0; i<students.length; i++){ // you are looping through rows and selecting the 1st and only column index
if (students[i][0] !== ''){
MailApp.sendEmail(students[i][0].toString(), subject ,body, {attachments:[{fileName:sheetName+".xlsx", content:contents, mimeType:"application//xlsx"}]});
}
}
}
Explanation:
My suggestion would be to just create a copy of the TempDataSet sheet and do all the sort and delete columns operations on that sheet:
var sh = SpreadsheetApp.getActive();
var sheet = sh.getSheetByName("TempDataSet");
var sheet1 = sheet.copyTo(sh).setName('TempDataSet_temp');
var shID = sheet1.getSheetId().toString();
sheet1.getRange(2, 1, sheet.getLastRow() -1, sheet.getLastColumn()).sort({column: 3, ascending: true});
var columns_delete = [7,5,4,2];
columns_delete.forEach(col=>sheet1.deleteColumn(col));
then you can delete the temporary sheet after you saved it in the result variable:
sh.deleteSheet(sh.getSheetByName('TempDataSet_temp'))
Solution:
function sendEmails(){
var sss = SpreadsheetApp.getActiveSpreadsheet();
var ssID = sss.getId();
var sheetName = sss.getName();
var sh = SpreadsheetApp.getActive();
var sheet = sh.getSheetByName("TempDataSet");
var sheet1 = sh.insertSheet('TempDataSet_temp');
sheet.getDataRange().copyTo(sheet1.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sheet.getDataRange().copyTo(sheet1.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
var shID = sheet1.getSheetId().toString();
sheet1.getRange(2, 1, sheet.getLastRow() -1, sheet.getLastColumn()).sort({column: 3, ascending: true});
var columns_delete = [7,5,4,2];
columns_delete.forEach(col=>sheet1.deleteColumn(col));
var subject = 'Your Attendance Record at BDU';
var body = 'Dear Student,'+ '\n\n' + 'Greetings! Please find the attendance record attached for your reference.' + '\n\n' + 'Thank you.';
var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=xlsx&id="+ssID+"&gid="+shID;
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
sh.deleteSheet(sh.getSheetByName('TempDataSet_temp'))
var sheet2 = sh.getSheetByName('StudentList');
var data = sheet2.getLastRow();
var students = [];
var students = sheet2.getRange(2, 6, data).getValues();
for (var i=0; i<students.length; i++){ // you are looping through rows and selecting the 1st and only column index
if (students[i][0] !== ''){
MailApp.sendEmail(students[i][0].toString(), subject ,body, {attachments:[{fileName:sheetName+".xlsx", content:contents, mimeType:"MICROSOFT_EXCEL"}]});
}
}
}

Error On Sending Email From Google App Script

I'm trying to send an email to a user form after the submit the form. But I got an error:
Uncaught at emailCode2 at processForm2
Is there anything I did wrong? Your respond would be appreciated.
Here's my .html code:
<script>
function handleFormSubmit(formObject) {
google.script.run.processForm2(formObject);
document.getElementById("myForm").reset();
alert("Your form is submitted. Thank you!");
}
</script>
Here's my .gs code:
function processForm2(formObject) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName("Sheet1");
var tz = ss.getSpreadsheetTimeZone();
var d = Utilities.formatDate(new Date(), tz, 'dd/MM/yyyy # HH:mm:ss');
var code = Math.floor(Math.random() * 99999) + 10000;
ws.appendRow([code, d, null, null, formObject.name, formObject.email,
formObject.things]);
emailCode2(code);
}
function emailCode2(code){
var url = "https://docs.google.com/spreadsheets/d/11X8e10TGQwzUQVJzTJ24JB1KMCTzkL5F5vObKrH0__k/edit#gid=0";
var ss = SpreadsheetApp.openByUrl(url);
var lastRow = ss.getLastRow();
var sheet= ss.getSheetByName("Sheet1");
var name = sheet.getRange(lastRow,5).getValue();
var emailAddress = sheet.getRange(lastRow,8).getValue();
var subject = 'Code';
var message = 'Dear ' + name + ',\n\nHere is your code: ' + code;
MailApp.sendEmail(emailAddress, subject, message);
}
How about this answer?
Modification points:
I think that the error means that an error occurs at Google Apps Script side.
I think that the reason of your issue might be the following part.
var ss = SpreadsheetApp.openByUrl(url);
var lastRow = ss.getLastRow();
var sheet= ss.getSheetByName("Sheet1");
In this case, lastRow is the 1st tab of the Spreadsheet. If Sheet1 is not the 1st tab, the last row might be different. Please be careful this. So please modify as follows.
var ss = SpreadsheetApp.openByUrl(url);
var sheet= ss.getSheetByName("Sheet1"); // Modified
var lastRow = sheet.getLastRow(); // Modified
If the active Spreadsheet is the same with var url = "https://docs.google.com/spreadsheets/d/11X8e10TGQwzUQVJzTJ24JB1KMCTzkL5F5vObKrH0__k/edit#gid=0";, after the value was appended with appendRow, the email address is retrieved the column "H". But the appended values has the email at the column "F". By this, the email is not declared. By this, an error occurs.
When above points are reflected to your script, it becomes as follows.
Pattern 1:
If the active Spreadsheet is the same with var url = "https://docs.google.com/spreadsheets/d/11X8e10TGQwzUQVJzTJ24JB1KMCTzkL5F5vObKrH0__k/edit#gid=0";, how about the following modification?
Modified script:
function processForm2(formObject) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName("Sheet1");
var tz = ss.getSpreadsheetTimeZone();
var d = Utilities.formatDate(new Date(), tz, 'dd/MM/yyyy # HH:mm:ss');
var code = Math.floor(Math.random() * 99999) + 10000;
ws.appendRow([code, d, null, null, formObject.name, formObject.email, formObject.things]);
emailCode2(code);
}
function emailCode2(code){
var url = "https://docs.google.com/spreadsheets/d/11X8e10TGQwzUQVJzTJ24JB1KMCTzkL5F5vObKrH0__k/edit#gid=0";
var ss = SpreadsheetApp.openByUrl(url);
var sheet= ss.getSheetByName("Sheet1"); // Modified
var lastRow = sheet.getLastRow(); // Modified
var name = sheet.getRange(lastRow,5).getValue();
var emailAddress = sheet.getRange(lastRow,6).getValue(); // Modified
var subject = 'Code';
var message = 'Dear ' + name + ',\n\nHere is your code: ' + code;
MailApp.sendEmail(emailAddress, subject, message);
}
Pattern 2:
If the active Spreadsheet is NOT the same with var url = "https://docs.google.com/spreadsheets/d/11X8e10TGQwzUQVJzTJ24JB1KMCTzkL5F5vObKrH0__k/edit#gid=0";, how about the following modification?
Modified script:
function processForm2(formObject) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName("Sheet1");
var tz = ss.getSpreadsheetTimeZone();
var d = Utilities.formatDate(new Date(), tz, 'dd/MM/yyyy # HH:mm:ss');
var code = Math.floor(Math.random() * 99999) + 10000;
ws.appendRow([code, d, null, null, formObject.name, formObject.email, formObject.things]);
emailCode2(code);
}
function emailCode2(code){
var url = "https://docs.google.com/spreadsheets/d/11X8e10TGQwzUQVJzTJ24JB1KMCTzkL5F5vObKrH0__k/edit#gid=0";
var ss = SpreadsheetApp.openByUrl(url);
var sheet= ss.getSheetByName("Sheet1"); // Modified
var lastRow = sheet.getLastRow(); // Modified
var name = sheet.getRange(lastRow,5).getValue();
var emailAddress = sheet.getRange(lastRow,8).getValue();
var subject = 'Code';
var message = 'Dear ' + name + ',\n\nHere is your code: ' + code;
MailApp.sendEmail(emailAddress, subject, message);
}
Note:
In this modification, it supposes that formObject of processForm2(formObject) has the correct values. If this has wrong values, an error might occur. Please be careful this.
Reference:
getLastRow()

Create a Sub sheet from Master Sheet, using import range, filter and match Google App Script

I want to fetch data from a google sheet data dump/master sheet. There is a sheet for each month. Import/match the data to columns that are in a different order in another sub spreadsheet and put all months on one sheet. Lastly, I want to filter the data by person, status and date, so only these items will show unless Cell H1 is populated. If cell H1 is populated and not null, all data that meets the criteria will be shown.
Master: https://docs.google.com/spreadsheets/d/1nIzCqQUL1K4HwYUGV0jxjJjLKSFaeqVn27f_pkoL9j0/edit?usp=sharing
Subsheet:https://docs.google.com/spreadsheets/d/1-V0F_pJTKFmP8jRBwpIzBe_sP7GbC-314oUB97N0K-0/edit#gid=0
function Importmatchfilter() {
// source sheet
var ss = ('1nIzCqQUL1K4HwYUGV0jxjJjLKSFaeqVn27f_pkoL9j0/edit#gid=1395833187');
var ssn1 = ss.getSheetByName('April'); ('A:Z')
var ssn2 = ss.getSheetByName('May'); ('A:Z')
var ssn3 = ss.getSheetByName('June'); ('A:Z')
var ssn4 = ss.getSheetByName('July'); ('A:Z')
var ssn5 = ss.getSheetByName('August'); ('A:Z')
var ssn6 = ss.getSheetByName('September'); ('A:Z')
var ssn7 = ss.getSheetByName('October'); ('A:Z')
var ssn8 = ss.getSheetByName('November'); ('A:Z')
var ssn9 = ss.getSheetByName('December'); ('A:Z')
// Get full range of data
var SRange1 = ssn1.getDataRange();
var SRange2 = ssn2.getDataRange();
var SRange3 = ssn3.getDataRange();
var SRange4 = ssn4.getDataRange();
var SRange5 = ssn5.getDataRange();
var SRange6 = ssn6.getDataRange();
var SRange7 = ssn7.getDataRange();
var SRange8 = ssn8.getDataRange();
var SRange9 = ssn9.getDataRange();
// get A1 notation identifying the range
var A1Range = SRange1.getA1Notation();
var A1Range1 = SRange1.getA1Notation();
var A1Range2 = SRange2.getA1Notation();
var A1Range3 = SRange3.getA1Notation();
var A1Range4 = SRange4.getA1Notation();
var A1Range5 = SRange5.getA1Notation();
var A1Range6 = SRange6.getA1Notation();
var A1Range7 = SRange7.getA1Notation();
var A1Range8 = SRange8.getA1Notation();
var A1Range9 = SRange9.getA1Notation();
// get the data values in range
var SData1 = SRange1.getValues();
var SData2 = SRange2.getValues();
var SData3 = SRange3.getValues();
var SData4 = SRange4.getValues();
var SData5 = SRange5.getValues();
var SData6 = SRange6.getValues();
var SData7 = SRange7.getValues();
var SData8 = SRange8.getValues();
var SData9 = SRange9.getValues();
// target sheet
var ts = tss.getSheetByName('Sheet1');
function listMajors() {
gapi.client.sheets.spreadsheets.values.get({
spreadsheetId: '1-V0F_pJTKFmP8jRBwpIzBe_sP7GbC-314oUB97N0K-0',
range: A1
}).then(function(response) {
var range = response.result;
if (range.values.length > 0) {
appendPre('Due date, Status, Address:');
for (i = 0; i < range.values.length; i++) {
var row = range.values[i];
appendPre(row[0] + ', ' + row[4]);
function getheaderValues(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet2');
var range = sheet.getRange("A:Z");
var allOfColumnAz = range.getValues()
for (var i = 1; i < 10 ; i++){
var sData = eval("SData"+i);
var lastRow = ts.getLastRow();
var numRows = sData.length;
var numCols = sData[0].length;
ts.getRange(lastRow+(lastRow > 0 ? 2 : 1), 1, numRows, numCols).setValues(sData);
}}

Passing a value to other sheet

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);
}

Categories

Resources