def write(self, range, data):
'''
clears the spreadsheet, then given a range of data will write that data into the google sheets
with the id self.spreadsheet_id
'''
self.ensure_service_exists()
self.service.spreadsheets().values().clear(spreadsheetId=self.spreadsheet_id, range='A1:Z4000',
body={}).execute()
value_range_body = {'values': data}
result = self.service.spreadsheets().values().update(spreadsheetId=self.spreadsheet_id,
range=f'{self.sheet_label}!{range}', valueInputOption=self.value_input_option,
body=value_range_body).execute()
return result
For some reason, all of my data is being aggregated, and then cleared. When I want the opposite to happen.
Also, does anyone know how to undo a border color change on the API as well?
I found out the answer, I was actually calling it multiple times and therefore deleting the prior data I posted
Related
I add rows to a Google Sheet via the API. For this, I use C# Google.API.Sheets.v4 NuGet package. I pretty much calculate the cells where I want to write in myself.
My problem is that when I reach the end of the worksheet, I don't know this in my application. There doesn't seem to be an error or anything. Data is just lost!
When I use my sheet without the API I would notice and just press this button:
I want to either add 1 row before writing one row in my application or recognize that the end of the worksheet is reached.
The code looks pretty much like the update code provided here:
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update
I use the spreadsheets.values.update functionality and write into a bottom row that doesn't yet exist
So again in short: I need the "more rows at bottom" functionality somehow in my application
Bonus Question: I lost some data because of this problem. Any ideas on how to recover it?
I found a sattisfying workaround. Instead of using the spreadsheets.values.update functionality I use the spreadsheets.values.append functionality as shown here.
It is important to set it to InsertDataOptionEnum.OVERWRITE to mimic the behaviour or you'll end up having each new cell in a separate row.
OLD ( uses .Update() )
SpreadsheetsResource.ValuesResource.UpdateRequest request = sheetsService.Spreadsheets.Values.Update(valueRange, spreadsheetId, range);
request.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
NEW ( uses .Append() )
//use AppendRequest instead of UpdateRequest to avoid lost lines on end of sheet
SpreadsheetsResource.ValuesResource.AppendRequest request = sheetsService.Spreadsheets.Values.Append(valueRange, spreadsheetId, range);
request.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.USERENTERED;
request.InsertDataOption = SpreadsheetsResource.ValuesResource.AppendRequest.InsertDataOptionEnum.OVERWRITE; //overwrite when writing cells or it will be new line per cell
I have spreadsheet and I'm basically going to use it for inventory tracking and management.
In one sheet I'll have an app on my phone fill a list when it reads
a QR code.
In the second sheet there is also a list with the
specific QR codes of each item.
I need to know when a cell in the first sheet has been updated and put that information in the following two cells.
I'm using this trigger to check if ANY cell in the document has been updated.
function cellEditTrigger()
{
ScriptApp.newTrigger('coolFunction').forSpreadsheet('awsomeSpreadsheetID').onEdit().create();
}
It works fine and it runs the 'coolFunction' function. However I can't seem to be able to get the time and date for the cells that have been edited and put that information in the next two cells.
I know I could use onEdit(e), but I still can't get the information from e.range and I would like my script to not have to be bound to the Spreadsheet.
Is there any way to accomplish this?
I've been working on an independent project for a client of mine. They wanted to produce a button that, upon the user-click, it would open up a user-form and have a variety of macro-related options to choose from: a drop-down list, checkbox, option select button, etc.
I created a test formula and submitted it to the client; they enjoyed it thoroughly and decided to sent me a file to 'copy & paste' my original code within their excel file.
Problem is; because I'm a tad bit inexperienced with VBA I've run into a problem where once I click the button - the user form doesn't show up.
Below is a Dropbox link of the original file I created and it's original code; as well as the file that I am trying to copy.
Any help would be all welcome and appreciated.
Link to dropbox: https://www.dropbox.com/sh/l1t37lz8uritrua/AAAdWPGvw0GDZ6hW4SwmbBdRa?dl=0
OriginalProject.xlsm has a form named honor_roll_form which contains 100 lines of code.
CopyOfOriginal.xlsm has a form named UserForm1 which contains no useful code.
I do not believe there is any method of directly copying user forms from one workbook to another. Instead
Within VB Editor of OriginalProject.xlsm, select honor_roll_form.
Click File then Export File and save the form on your desktop or where ever you like.
You will now have two files on your desktop; one with an extension of frm and one with an extension of frx.
Within VB Editor of CopyOfOriginal.xlsm, click File then Import file.
Import honor_roll_form.frm
When I try clicking button "Honor Roll", I get "Method or data member not found" for project1Box. I will investigate after dinner (18:57 here) unless you tell me you already know why I am getting this error.
Extra comments in response to request from OP
It is late here but I have started looking down sub execute_button_Click within the second CopyOfOriginal.xlsm. I will comment on what I see even if it is not directly relevant to the non-execution of the macro.
If you open the VB Editor and look on the left you will see the Project Explorer. Near the top you will see:
Microsoft Excel Objects
Sheet1 (Sheet1)
I have always found this confusing. The first “Sheet1” is Excel’s Id for the worksheet and cannot be changed. The second “Sheet1” is the default name for the worksheet which can be changed. You can write Sheet1.Range("A1") or Worksheets("Sheet1").Range("A1"). That is: you can reference a worksheet by its Id or its name. You have named a variable of type Worksheet as Sheet1. Using Excel’s names as variable names can lead to bizarre errors so it is important to avoid doing anything like this.
It is better to always use meaningful names. At the moment, you know what Sheet1 means but if you come back to this macro in six or twelve months will you remember. I would use a variable as you have but I would name it WshtCis208 or WshtVBAProg or something similar.
Set ID = Range(Sheet1.Cells(2, 1), Sheet1.Cells(52, 1)) could be written as:
With WshtCis208
Set ID = Range(.Cells(2, 1), .Cells(52, 1))
End With
Using With statements produces faster code and, almost always, code that it easier to read.
“52” is the current bottom row for this table. Will you amend the macro for them every time they add or remove a student? There are several techniques for finding the last row, none of which is perfect in every situation. The technique that is the most convenient most of the time is:
Const ColCis208Id as Long = 1
Const ColCis208MidTermExam as Long = 5
Dim RowCis208Last as Long
RowCis208Last = .Cells(.Rows.Count, ColCis208Id).End(xlUp).Row
At the moment, column 1 is the Id column. It is perhaps unlikely that the Id column will move but it is very likely that some of the others columns will move when some new column is identified as useful. Do you want to scan the code trying to decide which 5s refer to the MidtermExam column when a Project3 column is added?
Constants allow you to name literals that might change. It makes your code easier to read and saves so much pain when a value changes.
.Rows.Count gives the number of rows in a worksheet for the current version of Excel so .Cells(.Rows.Count, ColCis208Id) identifies the bottom cell of column 1. End(xlUp).Row says go up until you hit a cell with a value and returns its row number. It is the VBA equivalent of Ctrl+Up.
The next statement subjectCount = … fails because projectBox does not exist on the form. You have changed the captions but not the names.
As far as I can see the form fails to execute because you have started updating it but have not finished.
I have a Gridex with one column as a link column. When a user clicks a link the event triggered should then store several values from the selected row in several different variables.
I suppose the values could also be stored in the same variable if necessary. I do however need to get several values from the GridEx, getting just the value of the clicked link isn't going to work for my use case.
I'm sure it's a simple one liner but I can't find a solution.
So I've found an answer to my question. This seems to work for me
Dim Row1 As Janus.Windows.GridEX.GridEXRow
Row1 = GridEXSORCodes.CurrentRow
frmGeneric.Var = Row1.Cells("Column").Value.ToString
frmGeneric.Var = Row1.Cells("Column").Value.ToString
I'm using the Google Spreadsheet API to convert a document containing workers shifts into event calendars.
The only problem is that shifts are represented by merged cells according to days and hours (with days and hours as rows and different work slots as cols), and when I read a certain cell, which is merged and spans over 6 cells, I cannot get the cells certain width or its merged area.
For example:
If I try to get the values between (4C:4E) I will get "Bob, , ," and not "bob,bob,bob", and I cannot even find a way to know how many cells "bob" take.
Do you guys know how can I know how many cells the merged one spread to? Or at least it's total width.
Thanks in advance!
Download from google drive as html, see:
Get FontStyle information from Google spreadsheet into appengine
Drive driveService = new Drive.Builder(TRANSPORT, JSON_FACTORY, credential).build();
File file = driveService.files().get(this.spreadsheetKey).execute();
String downloadUrl = file.getExportLinks().get("application/pdf");
downloadUrl = downloadUrl.replaceFirst("exportFormat=pdf", "exportFormat=html");
downloadUrl = appendWorksheetGid(downloadUrl); // adds "&gid="+sheetGid
HttpResponse resp =
driveService.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl))
.execute();
System.out.println("downloadUrl:"+downloadUrl);
InputStream fileContent = resp.getContent();
extractStyleFromHtml(fileContent,downloadUrl);
extractStyleFromHtml uses Jsoup - (Jsoup impressed me)
It's not possible via spreadsheet API. I was looking for same. Google admits the same in their documentation.
The literal value of the cell element is the calculated value of the
cell, without formatting applied. If the cell contains a formula, the
calculated value is given here. The Spreadsheets API has no concept of
formatting, and thus cannot manipulate formatting of cells.
Related question:
Set cell format in Google Sheets spreadsheet using its API & Python
One alternate way can be to download the doc in excel format programmatically. It will contain formatting information. Then using excel API to extract the info.