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);
}
Related
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;
}
}
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")+
}
});
}
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)
}
When I've tried just to setValue("random") to some cell with this trigger it does work, but when I've tried to execute this code(for returning certain sheet names from selected spreadsheet) it doesn't work! When I run that chunk of code as separate function it does work and return sheets normally.
So, why it doesn't execute part of code from "//RETURNING JUST BUILDING LIST" part when I change value in D2 cell?
function onEdit(e) {
var range = e.range;
var rangeEdit = e.range.getA1Notation();
if(rangeEdit == "D2"){
//RETURNING JUST BUILDING LIST
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getSheetsByName("OVERVIEW").getRange('A2').setValue("OK")
var sRNG = ss.getSheetByName("KEYS").getRange('A1:A12').getValues();
for (var z=0; z<sRNG.length; z++)
if (sRNG[z][0] == ss.getSheetByName("OVERVIEW").getRange('G3').getValue()) {
var xyz = z + 1;
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = ss.getSheetByName("OVERVIEW").getRange(1, 8, 50, 1);
range.clear();
var shArray = [];
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var spreadSheetsInA =
SpreadsheetApp.openByUrl(ss.getSheetByName("KEYS").getRange('B' + xyz).getValue()).getSheets();
shArray = spreadSheetsInA.map(function(sheet) {
return [sheet.getName()];
});
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = ss.getSheetByName("OVERVIEW").getRange(1, 8, shArray.length, 1);
range.setValues(shArray);
var rDEL = 1
for (var xdel = 0; xdel<shArray.length; xdel++){
if(shArray[xdel][0].indexOf("Overview")>-1 | shArray[xdel][0].indexOf("Cashflow")>-1 | shArray[xdel][0].indexOf("Dates")>-1){
var delSHT = ss.getSheetByName("OVERVIEW");
delSHT.getRange('H' + rDEL).clear();
}
rDEL++
}
}
}
Even when I've tried this, it doesn't work...
function onEdit(e) {
var range = e.range;
var rangeEdit = e.range.getA1Notation();
if(rangeEdit == "D2"){
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getSheetsByName("OVERVIEW").getRange("A2").setValue("ok")
}
}
Basically, I just need to execute code if value in D2 in sheet Overview is changed!
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.