I already try everthing but I cant make this work =/
I'm trying to copy data from one spreadsheet to other using setValues(), because link doesn't work for me. I also need to keep the trigger on edit.
So, I create one fuction called AddConvocacao, and always when have any change the script run.
function addConvocacao(e){
var linha = e.range.getRow(); //get the number of the line, where have edit
var nome = e.source.getActiveSheet().getRange(linha,2).getValue(); //get the value of the field
var targetSheet = SpreadsheetApp.openById('1iQbZ7iB9ddc-HwLK9vG0eVEeWWqXKJZ0ry7U_Hm4SkQ') //open the other spreedsheet
var targetName = targetSheet.getSheetByName('Pac'); //open the tab
var lastRow = targetName.getLastRow(); //count the last row in this tab
lastRow = lastRow+1; //add 1
targetName.getRange(lastRow, 2).setValue(nome); // set the value
// targetName.getRange(lastRow, 3).setValue(valorCol7);
// targetName.getRange(lastRow, 5).setValue(dose);
// targetName.getRange(lastRow, 6).setValue(diagnostico);
// targetName.getRange(lastRow, 7).setValue(medico);
}
why dosen't work when I use on edit?
Thanks so much! =)
I did it a little different I think. I put checkboxes in column one and capture the value from column 2 and sent it to the other spreadsheet to the bottom of column 2
function onMyEdit(e) {
//e.source.toast('entry');
const sh = e.range.getSheet();
//Logger.log(JSON.stringify(e));
if (sh.getName() == 'Sheet1' && e.range.columnStart==1 && e.value=="TRUE") {
//e.source.toast('cond');
const v = sh.getRange(e.range.rowStart, 2).getValue();
var tss = SpreadsheetApp.openById(getGlobal('targetid'));
var tsh = tss.getSheetByName('Sheet1');
tsh.getRange(tsh.getLastRow() + 1, 2).setValue(v);
e.range.setValue("FALSE");
}
}
You must use an installable trigger to us openById();
getGlobal(targetid) just gets a spreadsheet id you can replace that with the id
Also I limited the action to only one sheet and only when I set the checkbox to true. I reset it back to false in the code.
You'll need to modify a few things to get it to work for you because when I'm doing the examples I do it my way.
Related
I'll try to keep this short and sweet.
I am making a packaging form using Google Sheets. Users fill out the form using the dropdowns on the spreadsheet and press the Submit button to send the data to Packaging Form Responses. This script works perfectly. Now I'm struggling to write a script to make it so that the checkbox in cell O13 auto-populates the data in the "Time Out" section in row 13 with the current time. You'll notice that I have the current time broken down into different cells on row 24 (hh , mm , am/pm). Basically, when the checkbox is = TRUE, I want the data in row 24 to be posted in row 13's "Time Out" section (side note: rows 22-24 will be hidden when the form is live). Haven't used onEdit() function very much so I could use some help here.
Here is my code thus far:
function onEdit(e) {
var range = e.range;
var spreadSheet = e.source;
var column = range.getColumn;
var row = range.getRow;
var hours = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Packaging Form').getRange('H24').getValue();
var minutes = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Packaging Form').getRange('I24').getValue();
var ampm = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Packaging Form').getRange('J24').getValue();
if(column == 15 && row == 13){
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Packaging Form').getRange('D13').setValue(hours);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Packaging Form').getRange('F13').setValue(minutes);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Packaging Form').getRange('H13').setValue(ampm)
}
}
Not sure what is wrong here, I believe it has to do with my local variables hours, minutes, and ampm.
Also I haven't gotten this far yet, but would like for the current time that gets posted in row 13 to be cleared when the checkbox no longer = TRUE (i.e. when the user un-checks the "Use current time" checkbox, the data in the "Time Out" section is cleared). Any help would be greatly appreciated, thank you.
Replace
var column = range.getColumn;
var row = range.getRow;
by
var column = range.getColumn();
var row = range.getRow();
or even better
var column = range.columnStart;
var row = range.rowStart;
P.S. you might also change SpreadsheetApp.getActiveSpreadsheet() by spreadsheet.
I created a function that's tied to a custom menu item in a google spreadsheet and is meant to detect the current row and perform some checking. The issue is that every time i run the menu item > script it always detect row 1 and not the current script. here's the code for reference:
function updateCalendarItems(){
//this function detects the current row and updates the main and follow up calendar events
//Get the current row
var ss = SpreadsheetApp.openById('<deleted>');
var sheet = ss.getSheetByName('db_clientvisit');
Logger.log('Sheet Name: '+sheet.getName()); //this logs the correct sheet name
// var range = sheet.getActiveCell();
var rowNum = sheet.getActiveCell().getRow();
Logger.log(rowNum); //always logs 1
var range = sheet.getRange(rowNum, 1, 1, 13).getValues(); //always returns the first row
Logger.log(range);
var ui = SpreadsheetApp.getUi();
ui.alert('Row Num is :'+rowNum+' Column Num is: '+sheet.getActiveCell().getColumn());
return;
var range = sheet.getRange(rowNum, 1, 1, 13);
var row = range.getValues()[0];
Logger.log(row);
//open the calendar
var calendarID = 'loveyourself.ph_umvj5k6vo45ei0mbh423g97ebg#group.calendar.google.com';
var calendar = CalendarApp.getCalendarById(calendarID);
//update the calendar...
}
Use var ss = SpreadsheetApp.getActive().
When you use SpreadsheetApp.openById('<deleted>'), you're essentially opening up a new session of the spreadsheet instead of your active session. So Apps Script isn't able to get the "active" ranges.
Similarly, you may also want to try using SpreadsheetApp.getCurrentCell().
function updateCalendarItems() {
var currentCell = SpreadsheetApp.getCurrentCell();
var rowNum = currentCell.getRow();
var ui = SpreadsheetApp.getUi();
ui.alert('Row Num is :'+rowNum+' Column Num is: '+currentCell.getColumn());
return;
}
I have a loop that does what I want, but I would like to set an active cell in each sheet so I don't have to click it each time as I cycle through.
I've tried two ways that seem to make sense, but they both only work on the last sheet in the loop:-
function setDraft() {
//msg box to confirm relevant sheets are hidden, thus excluded from code
var response = Browser.msgBox("SET AS DRAFT","Have you hidden sheets you don't want marked as draft?", Browser.Buttons.YES_NO);
if(response=="no")
return;
else if(response=="cancel")
return;
else
//loop and code for Visible sheets
var ss = SpreadsheetApp.getActive();
var allsheets = ss.getSheets();
for (var s in allsheets){
var sheet=allsheets[s]
var date = sheet.getRange('I1')
if (sheet.isSheetHidden()!= true) {
//sheet.setActiveRange(date);
sheet.setActiveSelection(date);
sheet.getRange('I5').setValue('DRAFT');
}
}
}
Can anyone please let me know where I'm going wrong?
Thanks in advance!
Use the sheet.activate() or range.activate() methods. Use sheet method if you only care to go to a specific sheet, but if you want the select a specific cell then use it with range. I have not tested with range, however, I know that if you use sheet.activate() then you will be brought to that sheet.
Using for...in in JS can lead to problems (more on that in this post). Second, try using the built in UI class through the SpreadsheetApp class rather than the Browser buttons.
function setDraft() {
var ui = SpreadsheetApp.getUi();
var allsheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
//msg box to confirm relevant sheets are hidden, thus excluded from code
var response = ui.alert("SET AS DRAFT","Have you hidden sheets you don't want marked as draft?", ui.ButtonSet.YES_NO);
if(response == ui.Button.NO) {
return;
} else {
for (var s=0; s<allsheets.length; s++){
var sheet=allsheets[s]
var date = sheet.getRange('I1')
if (sheet.isSheetHidden()!= true) {
sheet.setActiveSelection(date);
sheet.getRange('I5').setValue('DRAFT');
}
}
}
}
I currently have a jQuery Datatable, which upon a row being clicked on, the data from that row is outputted to textboxes and select boxes. I'm trying to make it so whatever is entered into the textboxes, will be saved/entered into the selected row upon pressing the saverow button.
Here's my JSFiddle: JSFiddle
Javascript:
var table = $('#example').DataTable();
(function () {
var table = document.querySelector('#example');
var name = document.querySelector('#nameinput');
var format = document.querySelector('#formatinput');
var address = document.querySelector('#addressinput');
var report = document.querySelector('#reportinput');
var alarm = document.querySelector('#alarminput');
table.addEventListener('click', onTableClick);
function onTableClick (e) {
var tr = e.target.parentElement;
var data = [];
for (var td of tr.children) {
data.push(td.innerHTML);
}
name.value = data[0];
address.value = data[1];
format.value = data[2];
report.value = data[3];
alarm.value = data[4];
console.log(alarm.value);
}
$("#saverow").click(function() {
var table1 = $('#data-table').DataTable();
var data = [];
data[0] = name.value;
data[4] = alarm.value;
console.log(name.value);
console.log(alarm.value);
table1.draw(true);
});
})();`
With the saverow code, I thought by trying to make the columns equal to the value of the textbox, then redrawing the table would work. The console does have the correct output when you type something new into the textbox then pressing Save. I just cant figure out how to put that back into the selected row.
I'm not wanting to do the inline editing if possible. Trying to keep it in this format.
I don't know if this counts as an answer, but you're not actually doing anything with that data variable in the save row click. You take the datatable, do nothing to change it, and then redraw it. So it's not surprising nothing is happening.
See this change to your fiddle to get the rows adding:
https://jsfiddle.net/o92g9goL/14/
Primarily, you need to set the table and datatable into different arrays. Also do it inside the main function. Also you need to actually add this code:
datatable.row.add(data).draw(false);
As for the editing, you'll need to make sure that you don't just prepopulate it, but make an actual reference to that row, otherwise how will it know to update it?
I am currently using google forms to enable users to submit site changes. This is all working beautifully.
I have added an extra column that has ticket status. Now I have written a script that checks for when this is updated to Done and then moves that row into another sheet entitled done.
However this only works if i update a row that i manually added. If I change the column on a row created from the form entry it will not work. Has anyone seen this issue before. Code is below.
function onEdit(e){
/*
When a user updates a status to done the ticket gets moved to another sheet
*/
var sheetNameToWatch = "Form responses 1"; // The sheet to watch for changes
var columnNumberToWatch = 1; // The column where the change happens on the above sheet
var valueToWatch = "Done"; // What is the text you are looking for
var sheetNameToMoveTheRowTo = "Done"; // The Sheet name for where the row gets moved to.
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get the active spreadsheet
var sheet = SpreadsheetApp.getActiveSheet(); // Get the active sheet
var range = sheet.getActiveCell(); // Get the current cell that has just been updated
// Check that you are on the correct sheet and column.
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo); // get a reference for the target sheet
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1); // get a reference for the target row
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange); // copy the row from current sheet to the new sheet
sheet.deleteRow(range.getRow()); // Delete the row from the old sheet
}
}
Yep, the onEdit function won't fire unless you do it manually, you'll want to use the onFormSubmit trigger, which will fire when a form response is received. This is a manually installed trigger connected to your form, which should be able to do the function above pretty much as written.
var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
ScriptApp.newTrigger('myFunction')
.forForm(form)
.onFormSubmit()
.create();
To be safe, though, you may want to define your spreadsheet, sheet, and range by id or by name rather than by whether they are active, I believe getActiveWhatever() kinds of methods will return null if the spreadsheet isn't open.
As mentioned, using active is not the best idea.
Instead of:
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get the active spreadsheet
var sheet = SpreadsheetApp.getActiveSheet(); // Get the active sheet
var range = sheet.getActiveCell(); // Get the current cell that has just been updated
Try:
var range = e.range;
var sheet = range.getSheet();
var ss = sheet.getParent();
If you try this you cannot execute it from the code editor because no event is passed. You must execute it by actually submitting a form.