I made a script that should insert two new rows in Google spreadsheets and copy paste the layout and values of two other rows.
But when I press the button I get a pop up window with "authorisation needed" and can't seem to fix it.
and this is the script that it should run:
function DuplicateSelectedRows() {
var spreadsheet = SpreadsheetApp.getActive();
//spreadsheet.getRange('B:B').activate();
// spreadsheet.setCurrentCell(spreadsheet.getRange('A1'));
//Insert rows before last training (row B and C)
spreadsheet.getActiveSheet().insertColumnsBefore(spreadsheet.getActiveRange().getColumn(), 1);
spreadsheet.getActiveRange().offset(0, 0, spreadsheet.getActiveRange().getNumRows(), 1).activate();
spreadsheet.getActiveSheet().insertColumnsBefore(spreadsheet.getActiveRange().getColumn(), 1);
spreadsheet.getActiveRange().offset(0, 0, spreadsheet.getActiveRange().getNumRows(), 1).activate();
// merge cells top
spreadsheet.getRange('B2:C2').activate()
.mergeAcross();
spreadsheet.getRange('B3:C3').activate()
.mergeAcross();
//spreadsheet.getRangeList(['B:B', 'C:C']).activate();
//spreadsheet.setCurrentCell(spreadsheet.getRange('A1'));
//spreadsheet.getRange('B4:C20').activate();
//paste values in newly created rows
spreadsheet.getRange('D4:E20').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
spreadsheet.getRange('B3:C3').activate();
spreadsheet.getRange('D3:E3').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
spreadsheet.getRange('B2:C2').activate();
spreadsheet.getRange('D2:E2').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
go back to the script editor and press run button (triangle-shaped button)
popup window will show up asking you to authorize the script for your spreadsheet
continue and ignore all warnings
when you finished authorizing your script go back to your spreadsheet and press your button
Related
I'm trying to create a rotation scheduler. My goal is to have a table of names, phone numbers and a button.
When the button ('Assign') is pressed, it highlights a row, and updates a cell outside the table with the date & time it was clicked. When it is pressed again, it un-highlights that row and highlights the next row, and updates the date/time cell.
When it reaches the bottom, it should move back to the top. It should save where it left off between closing and opening the document and it needs to sync even when several users have it open... I don't think Excel can do that, but Google Sheets should be able to, which is why I'm leaning towards Google Sheets.
I completely understand if that's too much to expect someone to sort out on a forum, but I would very much appreciate even a point in the right direction as to figuring it out myself!!
When the button ('Assign') is pressed, it highlights a row, and
updates a cell outside the table with the date & time it was clicked.
You can add or create an image that acts as a button and associate a script to be triggered when it is clicked.
When it reaches the bottom, it should move back to the top.
One way of doing it is to keep track of the highlighted row and use modular arithmetic to wrap back to the start of the table.
It should save where it left off between closing and opening the
document
One option is to use the Properties Service to store the highlighted row.
I tried to implement a sample using these ideas (make a copy before you run): https://docs.google.com/spreadsheets/d/1bHN5cf2HaLTW3EbAxfXUusQnDL3dNFY313J8cdTGp4o/
The associated script:
var NUMBER_OF_ASSIGNEES = 5;
var LAST_ASSIGNMENT_DATETIME_CELL = 'G3';
var TABLE_OFFSET = 2;
function assignNewPerson(){
var sheet = SpreadsheetApp.getActive().getActiveSheet();
setAssignTime(sheet);
assignNext(sheet);
}
function setAssignTime(sheet) {
var d = new Date();
sheet.getRange(LAST_ASSIGNMENT_DATETIME_CELL).setValue(d.toLocaleTimeString());
}
function assignNext(sheet) {
// Use the Properties Service to store the current assignee index.
var documentProperties = PropertiesService.getDocumentProperties();
var currentAssigneeIndex = parseInt(documentProperties.getProperty('CURRENT_ASSIGNEE_INDEX'));
if (isNaN(currentAssigneeIndex)) {
currentAssigneeIndex = 0;
}
// Remove highlight from assignee cells.
sheet.getRange(TABLE_OFFSET + currentAssigneeIndex, 1, 1, 2).setBackground("white");
// Increment the assignee index. If the last row is reached, go back to the start of the table.
currentAssigneeIndex = (currentAssigneeIndex + 1) % NUMBER_OF_ASSIGNEES;
documentProperties.setProperty('CURRENT_ASSIGNEE_INDEX', currentAssigneeIndex);
// Highlight assignee cells.
sheet.getRange(TABLE_OFFSET + currentAssigneeIndex, 1, 1, 2).setBackground("yellow");
}
Currently, I'm looking at some simple documentation for vague ways to make a 'button' (image) over a Google sheet to trigger a function on the script editor. I'm not familiar with this type of Syntax, I typically do AutoHotKey, and a bit of python.
All I want to do is have this button populate 2 columns. The current date in one, and the current time in the other (It doesn't even have to have its year or the seconds tbh). I don't know if it matters of what the pages name is based on how the script works. So the range is ( 'Log'!G4:H ).
Like if I were to make it for AutoHotkey I would put it as :
WinGet, winid ,, A ; <-- need to identify window A = active
MsgBox, winid=%winid%
;do some stuff
WinActivate ahk_id %winid%
So it affects any page it's active on.
I would like to use the same function on the same columns across different sheets. Ideally, that is. I don't care if I have to clone each a unique function based on the page, but I just can't even grasp this first step, lol.
I'm not too familiar with this new macro. If I use this macro does it only work for my client, because of say like it recording relative aspect ratio movements?
IE if I record a macro on my PC, and play it on my android. Will the change in the platform change its execution?
If anyone can point me in any direction as to any good documentation or resources for the Google Sheet Script Editor or its syntaxes I would really appreciate it.
EDIT: Just to clarify. Im really focused in on it being a function that populates from a click/press(mobile) of an image. I currently use an onEDIT on the sheet, and it wouldnt serve the purposes that I want for this function. Its just a shortcut to quickly input a timestamp, and those fields can still be retouched without it just reapplying a new function for a newer current time/date.
EDIT:EDIT: Ended up with a image button that runs a script that can only input to the current cell.
function timeStamp() {
SpreadsheetApp.getActiveSheet()
.getActiveCell()
.setValue(new Date());
}
It only works on the cell targeted.
I would like to force the input in the next availible cell in the column, and split the date from the time, and put them into cells adjacent from one another.
maybe this will help... if the 1st column is edited it will auto-print date in 2nd column and time in 3rd column on Sheet1:
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) {
var r = s.getActiveCell();
if( r.getColumn() == 1 ) {
var nextCell = r.offset(0, 1);
var newDate = Utilities.formatDate(new Date(),
"GMT+8", "MM/dd/yyyy");
nextCell.setValue(newDate);
}
if( r.getColumn() == 1 ) {
var nextCell = r.offset(0, 2);
var newDate1 = Utilities.formatDate(new Date(),
"GMT+8", "hh:mm:ss");
nextCell.setValue(newDate1);
}}}
https://webapps.stackexchange.com/a/130253/186471
I have a button in my parent frame(named as Add). I want to open a TextEntryDialog when user clicks that button,I want to open it like a pop up window.And i have to send that text typed by user back to parent frame, which i am going insert into my database table. It would be better if i can make a editable listctrl, in which i will have 4 columns and user will fill information into those columns and by sqlite query i will fill that information into database table.So how can i accomplish this?
First tutorial on the first page of a search for "wxpython text dialog"
https://pythonspot.com/en/wxpython-input-dialog/
You don't appear to have put in any effort whatsoever!
#!/usr/bin/python
import wx
def onButton(event):
print "Button pressed."
app = wx.App()
frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(0,0,200,50)
# Create text input
dlg = wx.TextEntryDialog(frame, 'Enter some text','Text Entry')
dlg.SetValue("Default")
if dlg.ShowModal() == wx.ID_OK:
print('You entered: %s\n' % dlg.GetValue())
dlg.Destroy()
I've been searching for similar solutions out there but am coming up short so far. Here is what I want to accomplish:
I need to come up with a basic solution to sync inventory quantities at the end of each day. We take physical counts of inventory sold throughout the day but need something to log these changes and share between users. I would like to utilize two buttons (click one to subtract amount of items sold at the end of the day and click one button to add newly received inventory).
This is how my sheet is set up:
Col A: Product Tag
Col B: Product sku
Col C: Amount Sold Today
Col D: Total Inventory Quantity
Col E: Add New Inventory
Column D will be pre-populated with initial inventory counts. At the end of each day, I would like to go down my product list and fill in the amount of each item sold that day in Column C. Once Column C is fully populated, I would like to click the "subtract" button and have Column C subtracted from Column D.
On the other side, once we receive new stock of an item I would like to enter these counts into Column E. Once this column is fully populated, I would like to click the "Add" button and have Column E added to Column D. Ideally once the add or subtract function has been completed, columns C or E will be cleared and ready for the next days entry.
I already have designed my buttons, I just need help coming up with the scripts to accomplish this.
You can use Google Apps Script for this.
If you are unfamiliar, in your particular spreadsheet, go to Tools → Script Editor and then select the Blank Project option.
Then you can write functions like this to achieve what you want!
function subtractSold() {
var sheet = SpreadsheetApp.getActiveSheet();
var c1 = sheet.getRange("C2");
var c2 = sheet.getRange("D2");
while (!c1.isBlank() && !c2.isBlank()){
c2.setValue(c2.getValue() - c1.getValue());
c1.clear();
c1 = c1.offset(1, 0);
c2 = c2.offset(1, 0);
}
}
Basically what the function does is:
Get a reference to the active spreadsheet
Get references to the cells C2 and D2, for the first row of data.
Use a while loop to repeated go through the rows. Terminate when either cell is empty.
In the loop, we get the appropriate values, subtract and set the value back into the cell. Then we clear the cell in column C. We then move both cell references down by one row (the offset method returns a reference to the original cell, but offset by row, column).
Then assign the script to the button image by entering the name of the function (subtractSold in this case) in the "Assign script" option for the button.
I have made an example sheet here (go to File → Make a Copy to try the scripts and see the code): https://docs.google.com/spreadsheets/d/1qIJdTvG0d7ttWAUEov23HY5aLhq5wgv9Tdzk531yhfU/edit?usp=sharing
A bit faster
If you try the sheet above you can see it processes one row at a time, which might get pretty slow when you have a lot of rows. It is probably faster to process the entire column in bulk, but it may be a bit more complicated to understand:
function subtractSoldBulk() {
var sheet = SpreadsheetApp.getActiveSheet();
var maxRows = sheet.getMaxRows();
var soldRange = sheet.getRange(2, 3, maxRows); // row, column, number of rows
var totalRange = sheet.getRange(2, 4, maxRows);
var soldValues = soldRange.getValues();
var totalValues = totalRange.getValues();
for (var row in soldValues) {
var soldCellData = soldValues[row][0];
var totalCellData = totalValues[row][0];
if (soldCellData != "" && totalCellData != "") {
totalValues[row][0] = totalCellData - soldCellData;
soldValues[row][0] = "";
}
}
soldRange.setValues(soldValues);
totalRange.setValues(totalValues);
}
The difference here is that instead of getting one cell, we get one range of cells. The getValues() method then gives us a 2D array of the data in that range. We do the calculations on the two arrays, update the data in the arrays, and then set the values of the ranges based on the array data.
You can find documentation for the methods used above from Google's documentation: https://developers.google.com/apps-script/reference/spreadsheet/sheet
When I move page01 to page02, I pass the same data along with it using the following code:
navigator.pushView(Page02, data);
How do I move to page02 with passing the next row of data (instead of the same data)?
In other word, how to increment to the next row of data with pushView?
Thanks.
If you have access to the List component which displays the data you want to pass into views, you can do something like this:
myList.dataProvider[myList.selectedIndex+1]
You'll want to do some checking to make sure that you're trying to reference an index that actually exists:
var mySelectedObject :Object;
if(myList.selectedIndex+1 < myList.dataProvider.length){
mySelectedObject = myList.dataProvider[myList.selectedIndex+1]
} else {
// do some other behaviour; such as selecting the first one in the list
mySelectedObject = myList.dataProvider[0]
}
navigator.pushView(page02, mySelectedObject );