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);
}
function saveData(){
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var formSheet = spreadsheet.getSheetByName("Receipt");
var dataSheet = spreadsheet.getSheetByName("Historical Sales")
var fieldRange = \["B12:X"\]
var fieldValue = fieldRange.map(f =\>
formSheet.getRange(f).getValue())
//console.log(fieldValue)
dataSheet.appendRow(fieldValue)
};
function exp5() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('W12').activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.PREVIOUS).activate();
currentCell.activateAsCurrentCell();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Historical Receipts'), true);
spreadsheet.getRange('X963').activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.PREVIOUS).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
spreadsheet.getRange('A14:V23').activate();
spreadsheet.getRange('Receipt!B12:W21').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
This is how I'd do it:
//Not tested, but it can give you a direction
function moveData () {
var formSheet = spreadsheet.getSheetByName("Receipt");
var formRng = formSheet.getRange(); //Specify the data range here
var formData = formRng.getValues();
var dataSheet = spreadsheet.getSheetByName("Historical Sales");
//It gets the first empty row in column A and "pastes" the data set (form Data) .
datasheet.getRange(datasheet.getLastRow()+1,1,formData.length,formData[0].length).setValues(formData);
}
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")+
}
});
}
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!
I have a project I been working on to read data from one sheet and iterate through the designated columns grab that data and place it on a template that will be uploaded to Google ads. See the code below (A SIMPLE LOOP IS NEEDED SEE MY CODE FOR COMMENTS!). Please note that i'm new to this and it may look like an eye sore. I been stuck on this for a while now trying literally everything I found with no success. if you need additional attachments or clarification please let me know.
//============================================================================*
// This program is an inventory based program that will create AdGroups based *
// On your current inventory Spreadsheet on your G-Drive *
// ===========================================================================*
function main() {
var feedCSVname = "FILE_NAME.csv"
var feedSheetName = "INVENTORY SHEET"
var TemplateURL = "SHEET_URL_HERE"
var Template_sheetName = "TEMPLATE_SHEET_NAME"
var push_sheetName = "Upload"
var done = "Done"
// ==== Parse CSV file and dump all contents to inventory sheet =========
var feedFile = DriveApp.getFilesByName(feedCSVname).next();
var feedData = Utilities.parseCsv(feedFile.getBlob().getDataAsString());
var Template = SpreadsheetApp.openByUrl(TemplateURL);
var feedSheet = Template.getSheetByName(feedSheetName);
feedSheet.clear().getRange(1, 1, feedData.length, feedData[0].length).setValues(feedData);
// Create a new columns "Status" so we can loop through
feedSheet.getRange('BI1').setValue('Status');
//==================== Feed Column Locations ============================
var CidColumn = 2;
var sellingPriceColumn = 10;
var yearColumn = 13;
var makeColumn = 14;
var modelColumn = 15;
var trimColumn = 16;
var dealernameColumn = 39;
var countofmakemodelyearColumn = 47;
var lowest_priceColumn = 52;
var mm_lowest_priceColumn = 53;
var m_lowest_priceColumn = 54;
var URL1Column = 58;
var URL2Column = 59;
var URL3Column = 60;
var status = 61;
var feedRows = 2 // Since row 1 has headers
var lastrow = feedSheet.getLastRow();
//======================= Feed Row Data ================================
var sellingPrice = feedSheet.getRange(feedRows, sellingPriceColumn).getValue();
var year = feedSheet.getRange(feedRows, yearColumn).getValue();
var make = feedSheet.getRange(feedRows, makeColumn).getValue();
var model = feedSheet.getRange(feedRows, modelColumn).getValue();
var dealername = feedSheet.getRange(feedRows, dealernameColumn).getValue();
var URL1 = feedSheet.getRange(feedRows, URL1Column).getValue();
var URL2 = feedSheet.getRange(feedRows, URL2Column).getValue();
var URL3 = feedSheet.getRange(feedRows, URL3Column).getValue();
//==================== Write Data To Template ==========================
// I need to loop this process
var templateSheet = Template.getSheetByName(Template_sheetName);
templateSheet.getRange('B2').setValue("Campaign Name");
templateSheet.getRange('B3').setValue(make);
templateSheet.getRange('B4').setValue(model);
templateSheet.getRange('B5').setValue(year);
templateSheet.getRange('D2').setValue(URL1);
templateSheet.getRange('D3').setValue(URL2);
templateSheet.getRange('D4').setValue(URL3);
feedSheet.getRange(feedRows, status).setValue(done);
// when this column and row = "Done" then go to next row
var readStatusColumn = feedSheet.getRange(feedRows, status).getValue();
if (readStatusColumn == done) {
//Then go to next row loop process
}
//================ Copy all data to push sheet =========================
//Copy contents of the Template sheet and paste to the Upload sheet
var copyRange = templateSheet.getRange('A8:J113');
var pushSheet = Template.getSheetByName(push_sheetName);
var paste2pushSheet = pushSheet.getRange(pushSheet.getLastRow() + 1, 1);
copyRange.copyTo(paste2pushSheet, {
contentsOnly: true
});
//End Loop
}