3

I have linked part of a sheet in google doc (as linked object). Now, whenever I change the sheet data, I can click a button in google doc and the data is reflected in the google doc linked sheet too (this is all built in google doc stuff).

What I want to do is the other side of this. I am able to see a bunch of data in one place (google doc) based on the sheets I have linked. I would like to update the data in the google doc, and "upload" it to the linked google sheets.

I am trying to write a script to do that. But cannot seem to find any method to access linked sheets. I found this slides API page that can does the sheet -> slide syncing.

I am looking at the document API page, but I scanning through add... and get... methods, I don't see to find any way to get linked objects. Is it represented as NamedRange? If so, how do I access it?

There was another similar question, but without any satisfactory answer.

If you can share some pointers to get started, I would appreciate it.

Edit: Here is an example doc (and a spreadsheet contained their in) to explain the situation clearer. Test document for updating spreadsheet - Google Docs

6
  • If I understand correctly, you want to update a spreadsheet by editing the sheet data linked in the Doc? I don't think that's possible. Also, the Slides method you provided doesn't do that either: it just refreshes the chart based on changes made in the sheet. If, instead, you just want to be able to refresh the linked data via API based on edits made to the sheet, I'd suggest you to file a feature request in this Issue Tracker component. Commented Sep 4, 2020 at 8:37
  • You got it right, @Iamblichus, I want to update the sheet based on changes made in the linked area in google doc. When you say it is not possible, do you mean that there is no API to get the linked ranges in google doc? Commented Sep 6, 2020 at 19:12
  • If you provide more information on your specific situation (including a sample Doc and a Spreadsheet, free of sensitive data), people might be more willing to delve into this. Commented Sep 8, 2020 at 9:58
  • @Iamblichus: I updated the question with a sample document. Thanks for helping me ask questions in a clearer way. Let me know if this helps, and if it does, what information can I provide to make it helpful. Commented Sep 10, 2020 at 4:12
  • 1
    Hi, the file you shared is not public. Can you make it public? Commented Sep 10, 2020 at 11:44

1 Answer 1

4

You can find the Table elements in your Document via findElement(elementType). If that's the only Table in your Document, as in the sample you shared, this is immediate.

Once you retrieve the Table, you can loop through its rows and cells and come up with a 2D array with the values from the table:

function getTableValues() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  const table = body.findElement(DocumentApp.ElementType.TABLE).getElement();
  let tableValues = [[]];
  for (let i = 0; i < table.getNumRows(); i++) {
    const tableRow = table.getRow(i);
    for (let j = 0; j < tableRow.getNumCells(); j++) {
      const tableCell = tableRow.getCell(j);
      const text = tableCell.getText();
      tableValues[i].push(text);
    }
    if (i == table.getNumRows() - 1) break; 
    tableValues.push([]);
  }
  return tableValues;
}

Once you've done that, you just need to copy it to your spreadsheet via setValues(values):

function copyToSheet(tableValues, spreadsheetId) {
  const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName("Sheet1");
  sheet.getRange(1, 1, tableValues.length, tableValues[0].length).setValues(tableValues);  
}

Calling both of these in the same function, you would get this:

function main() {
  const tableValues = getTableValues();
  const spreadsheetId = "{your-spreadsheet-id}";
  copyToSheet(tableValues, spreadsheetId);
}

Note:

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Iamblichus for the detailed code. In my actual document, I have more tables, but I can loop through them. The range that is linked in the document is not "A1:...", but some other range in the spreadsheet. So, the two question remain: (1) How do I get the spreadsheet-id (programatically) that is embedded in the document, and (2) How do I get the range (e.g., in the example document case, it is B2:D4)?
Also, when the table cell contains bullet points, the getText() method does not give an indication that there are bullet points (e.g., '*' in the beginning of the line). It just give me bullet points separated by newline. Is there a way to get bullet points from the cell?
I added another range from the spreadsheet to the document, to make the problem clearer. I can loop over tables myself, so please don't spend time writing that functionality. The question now really is about how to get the spreadsheet id and associated range?
@YogeshwerSharma My apologies. No, there is no way to get the ID and the range of the spreadsheet from the Docs, AFAIK. I totally missed that part from your question. You might want to file a Feature Request for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.