Why does my loop doesn't work on GoogleSheets? - javascript

So, I've been trying to do this: https://wafflebytes.blogspot.com/2017/06/google-script-create-calendar-events.html
I've pasted this code, and it worked quite nicely (the only downside is that the eventID always took the last column, instead of the one I choose).
Then I wrote this code:
function createCalendar() {
var sheet = SpreadsheetApp.getActiveSheet();
var calendar = CalendarApp.getCalendarById('YES MY ID IS HERE BUT I REMOVED TO POST');
var startRow = 2;
var numRows = sheet.getLastRow();
var numColumns = sheet.getLastColumn();
var dataRange = sheet.getRange(startRow, 1, numRows-1, numColumns);
var data = dataRange.getValues();
var complete = "on";
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var aplicador = row[2];
var data = new Date(row[9]);
var datafim = new Date(row[11]);
var eventID = row[22];
if (eventID != complete) {
var currentCell = sheet.getRange(startRow + i, numColumns);
calendar.createEvent(aplicador, data, datafim);
currentCell.setValue(complete);
}
}
console.log(data.length)
}
which is exactly the same code, except I changed some variables. And it doesn't work.
It does well on the first line, but the loop doesn't happen.
Why is that?

You declared data twice once inside the loop and once for all of the data on the spreadsheet.
Try it this way:
function createCalendar() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const calendar=CalendarApp.getCalendarById('YES MY ID IS HERE BUT I REMOVED TO POST');
const shsr=2;
const rg=sh.getRange(shsr,1,sh.getLastRow()-shsr+1,sh.getLastColumn());
var data=rg.getValues();
const complete="on";
const lc=sh.getLastColumn();
data.forEach(function(r,i){
let aplicador=r[2]
let dts=new Date(r[9])
let dte=new Date(r[11]);
let eventId=r[22];
if(eventId!="on") {
calendar.createEvent(aplicador,dts, dte);
sh.getRange(shsr+i,22).setValue("on")+
}
});
}

Related

Apps Script: how to copy hyperlink from a cell to another sheet

I need to copy this data from one sheet to another but with all the links active, because with this formula, copy just like a text.
I really don't know what do and what do I have to change to copy every data from the original sheet.
function SubmitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("ProyectStatus");
var dataS = ss.getSheetByName("ToCompleteProyect");
var values = [
[formS.getRange("D1").getValue(), formS.getRange("D2").getValue(), formS.getRange("D3").getValue(), formS.getRange("D4").getValue(), formS.getRange("D5").getValue(), formS.getRange("D6").getValue(), formS.getRange("D7").getValue(), formS.getRange("D8").getValue(), formS.getRange("D9").getValue(), formS.getRange("D10").getValue(), formS.getRange("D11").getValue(), formS.getRange("D12").getValue(), formS.getRange("D13").getValue(), formS.getRange("D14").getValue(), formS.getRange("D15").getValue(), formS.getRange("D16").getValue(), formS.getRange("D17").getValue(), formS.getRange("D18").getValue(), formS.getRange("D19").getValue(), formS.getRange("D20").getValue(), formS.getRange("D21").getValue(), formS.getRange("D22").getValue(), formS.getRange("D23").getValue(), formS.getRange("D24").getValue(), formS.getRange("D25").getValue(), formS.getRange("D26").getValue(), formS.getRange("D27").getValue(), formS.getRange("D28").getValue(), formS.getRange("D29").getValue(), formS.getRange("D30").getValue(), formS.getRange("D31").getValue(), formS.getRange("D32").getValue(), formS.getRange("D33").getValue(), formS.getRange("D34").getValue(), formS.getRange("D35").getValue(), formS.getRange("D36").getValue(), formS.getRange("D37").getValue(), formS.getRange("D38").getValue(), formS.getRange("D39").getValue(), formS.getRange("D40").getValue(), formS.getRange("D41").getValue(), formS.getRange("D42").getValue(), formS.getRange("D43").getValue(), formS.getRange("D44").getValue(), formS.getRange("D45").getValue(), formS.getRange("D46").getValue(), formS.getRange("D47").getValue(), formS.getRange("D48").getValue(), formS.getRange("D49").getValue(), formS.getRange("D50").getValue(), formS.getRange("D51").getValue(), formS.getRange("D52").getValue()]
];
dataS.getRange(dataS.getLastRow() + 1, 1, 1, 52).setValues(values);
ClearCell();
RE-EDIT:
Complete project with Submit, Search and Update buttons!
function ClearCell() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("ProyectStatus");
var RangeToClear = [ "D1:D52"];
for (var i=0; i<RangeToClear.length; i++) {
formS.getRange(RangeToClear[i]).clearContent();
}
}
//-------------------------------------------------------------------------------
function SubmitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("ProyectStatus");
var dataS = ss.getSheetByName("ToCompleteProyect");
var lastRow = dataS.getLastRow() + 1
formS.getRange("D1:D52").copyTo(dataS.getRange(lastRow, 1, 1, 52), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true)
dataS.getRange(lastRow,1).clearDataValidations()
ClearCell();
}
//----------------------------------------------------------------------------
function Search() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("ProyectStatus");
var str = formS.getRange("D1").getValue();
var dataS = ss.getSheetByName("ToCompleteProyect")
var values= dataS.getDataRange();
Logger.log(values.getLastRow())
for (var i = 0; i < values.getLastRow(); i++) {
var row = dataS.getRange(i+1,1).getValue();
if (row == str) {
dataS.getRange(i+1,2,1,51).copyTo(formS.getRange("D2:D52"),SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true)
}
}
}
//------------------------------------------------------------------------------
function Update(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("ProyectStatus");
var str = formS.getRange("D1").getValue();
var dataS = ss.getSheetByName("ToCompleteProyect")
var values= dataS.getDataRange();
Logger.log(values.getLastRow())
for (var i = 0; i < values.getLastRow(); i++) {
var row = dataS.getRange(i+1,1).getValue();
if (row == str) {
formS.getRange("D2:D52").copyTo(dataS.getRange(i+1, 2, 1, 51), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true)
ClearCell();
}
}
}
EDIT:
One simple way to do it is just to copy-paste but not only the values but the cells transposed as one would do with the Copy and Paste Special. It's indeed a much simpler code and it seems to me that does the trick, you just need this line:
formS.getRange("D1:D52").copyTo(dataS.getRange(lastRow, 1, 1, 52), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true) ---> this last TRUE value is the one which transposes the data
And the full function:
function SubmitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("ProyectStatus");
var dataS = ss.getSheetByName("ToCompleteProyect");
var lastRow = dataS.getLastRow() + 1
formS.getRange("D1:D52").copyTo(dataS.getRange(lastRow, 1, 1, 52), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true)
ClearCell();
}
Was it useful??
OLD: If the links is always in the same column/s of the last row, you can then pass them through this second function. I took myself the liberty also of suggesting a way of simplifying grabbing the values of the form ;)
function SubmitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("ProyectStatus");
var dataS = ss.getSheetByName("ToCompleteProyect");
var values = formS.getRange("D1:D52").getValues() //Here I took the entire range
values = [values.map(function(n){return n[0]})] // and turn that column into a row
var lastRow = dataS.getLastRow() + 1
dataS.getRange(lastRow, 1, 1, 52).setValues(values);
linkCellContents(dataS.getRange(lastRow, 10, 1, 1)) // assuming you need to make a link the value in column number 10 (J)... repeat this line with each column you need to convert
ClearCell();
}
function linkCellContents(range) {
var value = range.getValue()
var richValue = SpreadsheetApp.newRichTextValue()
.setText(value)
.setLinkUrl(value)
.build();
range.setRichTextValue(richValue);
}

Stop google script data import when parsing an empty row

I would like data import to stop when parsing an empty row. I have tried this code but it still imports data past empty lines:
function readInAllData() {
var threads = GmailApp.search("subject:Report #7");
var message = threads[0].getMessages()[threads[0].getMessages().length-1];
var attachment = message.getAttachments()[0];
if (attachment.getContentType() === "text/csv") {
attachment.setContentTypeFromExtension();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Y input");
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
var range = sheet.getRange("A:R");
var row = 0;
range.clearContent();
for (var row=0; row<csvData.length; row++) {
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
if (!csvData[row].join("")) break;
}
return null;
}
Explanation:
You don't need a for loop to find the first empty row in your data. You can use findIndex and every to find the first row for which every cell is empty and then set the values up to this row:
const pos = csvData.findIndex(r => r.every(c=>c=='') );
sheet.getRange(1, 1, pos, csvData[0].length).setValues(csvData.slice(0,pos));
Solution:
function readInAllData() {
var threads = GmailApp.search("subject:Report #7");
var message = threads[0].getMessages()[threads[0].getMessages().length-1];
var attachment = message.getAttachments()[0];
if (attachment.getContentType() === "text/csv") {
attachment.setContentTypeFromExtension();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Y input");
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
var range = sheet.getRange("A:R");
range.clearContent();
const pos = csvData.findIndex(r => r.every(c=>c=='') );
sheet.getRange(1, 1, pos, csvData[0].length).setValues(csvData.slice(0,pos));
return null;
}
}

changing the width when running a function

I'm relatively new to coding, so thanks in advance for any assistance here.
I have a script that runs two functions on the same sheet.
Button2 contains a function (email_button2) that runs a SQL query and pulls info into columns A to D
Button1 also contains a function (email_button1) that runs a separate query and I want to pull this info in columns G to AA (21 columns)
Right now, when I click Button1, I get the following error: Incorrect range width, was 1 but should be 21
Any idea what I should change or add to my script?
function data_button() {
// Logger.log(e)
var thisDoc = SpreadsheetApp.getActiveSpreadsheet();
var helpers = thisDoc.getSheetByName("helper");
query =
helpers.getRange(3,2).getValue().split(String.fromCharCode(13)).join(" ").split(String.fromCharCode(10)).join(" ")
// lastrow = helpers.getRange("lastrow").getValue()
do_query(query,"data")
}
function email_button1() {
// Logger.log(e)
var thisDoc = SpreadsheetApp.getActiveSpreadsheet();
var helpers = thisDoc.getSheetByName("helper");
query =
helpers.getRange(4,2).getValue().split(String.fromCharCode(13)).join("
").split(String.fromCharCode(10)).join(" ")
// lastrow = helpers.getRange("lastrow").getValue()
do_query(query,"emails",11,7)
}
function email_button2() {
// Logger.log(e)
var thisDoc = SpreadsheetApp.getActiveSpreadsheet();
var helpers = thisDoc.getSheetByName("helper");
query =
helpers.getRange(5,2).getValue().split(String.fromCharCode(13)).join("
").split(String.fromCharCode(10)).join(" ")
// lastrow = helpers.getRange("lastrow").getValue()
do_query(query,"emails",11,1)
}
function do_query(query,sheetname,startRow,startCol){
//List of lists?
var url= "xxxxxxxxxxxxxx"
var q = {"query":query}
var options = {
'method' : 'post',
'payload' : q,
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(url, options); // get feed
var response2 = response.getContentText()
var rows = response2.split(";;;")
var columnCount = rows[0].length
var data = [];
for(var i = 0; i < rows.length; i++){
var row = rows[i].split('|||');
data.push(row);
}
Logger.log(data)
var a = data[1]
var thisDoc = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = thisDoc.getSheetByName(sheetname);
sheet2.getRange(startRow,startCol,200,4).clear()
sheet2.getRange(startRow,startCol, data.length,
data[0].length).setValues(data);
//this line above designates column headers
//sheet2.getRange(12, 1, data[1].length, data[1] .
[0].length).setValues(data[1]);
//Logger.log(data[0].length)
//Logger.log(data[0][0].length)
//Logger.log(data[0])
// thisDoc.getSheetByName("helper").getRange("lastrow").setValue(data.length)
}

Google Script: Script to create calendar event runs without error but does nothing

function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var calendarName = "CALENDAR";
var calTimeZone = CalendarApp.openByName(calendarName).getTimeZone();
var menuEntries = [ {name: "Update Calendar", functionName: "cal1"} ];
ss.addMenu("Calendar", menuEntries);
ss.setSpreadsheetTimeZone(calTimeZone);
}
function cal1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName("Sheet1");
var calendarName = "CALENDAR";
var calTimeZone = CalendarApp.openByName(calendarName).getTimeZone();
var startRow = 3;
var numRows = 100;
var dataRange = dataSheet.getRange(startRow, 1, numRows, 26);
var data = dataRange.getValues();
var cal = CalendarApp.getCalendarsByName("CALENDAR")[0];
for (i in data) {
var row = data[i];
if (row[3] == "Add") {
var title = row[24]; // Column with Title
var desc = row[21]; // Column with Description
var startdate = row[12]; //Column with Date
var enddate = row[14]; //Column with Location
cal.createEvent(title, startdate, enddate);
}
}
}
This is a simple script that's supposed to look through my spreadsheet and add it to my calendar if column 3 is "Add". The code runs without any error but when I check my calendar there is nothing there. Am I doing something wrong here?
After checking the logger I realised that the indexing starts from 0 (column C = row[2], not row[3]). It's a silly mistake, and as much as I'd like to delete this question away I thought it would be good to put this out there in case someone does the same thing.

Google Sheets Script for passing a few cell values on one sheet to new sheet row

I have looked and looked and tried to adapt some scripts but am stuck.
Can someone help/show me how to add a collection of specific cells on one sheet and populate the next available row on a second sheet with these values? Please. This is what I have:
function AddToDB2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("PurchaseOrder");
var targetSheet = target.getSheetByName("DataBase");
var range = ss.getDataRange();
var values = range.getValues();
var date1 = values[1][6]; //these values are collected from multiple cells not in a specific row or col
var po = values[2][6];
var vendor = values[7][0];
var shipto = values[7][3];
//this was for testing that the values were grabbed correctly.
var data = [date1,po,vendor,shipto];
// I don't think this part is even close
var lastRow = targetSheet.getLastRow()
var rowData = values[0]
var newData = []
for(n=1;n<lastRow;++n){newData.push(rowData)}
targetSheet.getRange("C2:H"+lastRow).setValues(newData);
}
This is the other way I came up with:
function addToDB() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("PurchaseOrder");
var targetSheet = target.getSheetByName("DataBase");
var sourceRangePO = sourceSheet.getRange("G3");
var poValues = sourceRangePO.getValues();
if(poValues != "") {
var sourceRangeDate = sourceSheet.getRange("G2");
var targetRangeDate = targetSheet.getRange("A2");
var dateValues = sourceRangeDate.getValues();
targetRangeDate.setValues(dateValues);
var sourceRangePO = sourceSheet.getRange("G3");
var targetRangePO = targetSheet.getRange("B2");
var poValues = sourceRangePO.getValues();
targetRangePO.setValues(poValues);
var sourceRangeVendor = sourceSheet.getRange("A8");
var targetRangeVendor = targetSheet.getRange("C2");
var vendorValues = sourceRangeVendor.getValues();
targetRangeVendor.setValues(vendorValues);
var sourceRangeShipTo = sourceSheet.getRange("D8");
var targetRangeShipTo = targetSheet.getRange("D2");
var shipToValues = sourceRangeShipTo.getValues();
targetRangeShipTo.setValues(shipToValues);
targetSheet.insertRowAfter(1);
Browser.msgBox("Your PO has beed saved!");
}
else { Browser.msgBox("Please enter a PO number"); }
}
You were very close. Try this. I was a little confused by your setValues C:H. You only have 4 values to set. I set them in D-G. You can adjust as needed.
function AddToDB2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("PurchaseOrder");
var targetSheet = ss.getSheetByName("DataBase");
var range = sourceSheet.getDataRange();
var values = range.getValues();
var date1 = values[1][6]; //these values are collected from multiple cells not in a specific row or col
var po = values[2][6];
var vendor = values[7][0];
var shipto = values[7][3];
var data = [date1,po,vendor,shipto];
var lastRow = targetSheet.getLastRow()
var newData = []
for(n=0;n<data.length;++n){newData.push([data[n]])}
var rng =targetSheet.getRange(lastRow+1,1,1,4) // Col A-D
rng.setValues([newData]);
}

Categories