Hello I wanted to copy a particular row from one spreadsheet to another spreadsheet using google apps script.Can anyone please help me to get the answer for this.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- How can I force all files in a folder to be owned
Use range.setValues, is the best choice. Works for any range in any sheet, as far as the origin and destination range are similar.
Here's a sample code:
I ran into this as well. I managed to figure out a nice workaround that copies everything from one sheet to another (except for images, graphs, scripts and stuff). It copies formulas, values, formating for sure.
Basically the solution is to
Code:
Based on the above I created a spreadsheet that allows me to update many copies from a master spreadsheet. With about 15 copies, it saves a lot of copy-paste actions.
Maybe this post helps you http://l.rw.rw/gscripts3. It describes functionality of copy function with different conditions (limit data by range, custom filters, place data to target with insert or replace methods). And has working example where you can try it.
Check out the documentation here:
http://code.google.com/googleapps/appsscript/service_spreadsheet.html
Let's assume you're working in the spreadsheet where you're copying from.
You'd have to get a handle to the current spreadsheet and the target spreadsheet. You'll need to get the ID for the target spreadsheet. Details are in the link up there.
Next we need to pick the particular sheets within those spreadsheets. Let's say your row is on the sheet named "New Stuff", and you have a sheet in the target spreadsheet named "Archive".
Now, the concept that google apps spreadsheets use are ranges. A range is just a chunk of cells. So we need to determine the from-range and the to-range. Let's say your sheet has 7 columns and you want the 10th row.
So we're going to take that row and put it on the first row of the target sheet. Now for the actual copy:
And you're done!
Now, this will always clobber the first row of the target sheet. There's plenty you can do to stop that. Instead of always using the first line, you could use the sheet object methods to find the last row, add one after, and then use it as your range.
That way each time you copy a row over, it just gets added to the bottom of the sheet.
Another way of doing it would be to import the row (or any range) from the source into the target spreadsheet.
Assuming that you want to import row 3 from source spreadsheet's sheet called "source-spreadsheet-sheet-name", put in the first column of the target location the following formula:
where
"source-spreadsheet-key"
is the unque ID of the source spreadsheet that you extract from its URL.If you do it first time, the security message will appear:
Just confirm and the whole row will be filled in automatically.
Please remember that sometimes it takes couple of minutes to update the imported cells after changes have been made in the source area. However you can speed it up by pressing ctrl-E, or ctrl-shift-E, while beeing in the target spreadsheet, whichever works.
This does not work - returns an error "Target range and source range must be on the same spreadsheet." Looks like we need to do something similar to this:
(blatantly stolen from here)