Append new ListItem to particular list in Google Docs API - google-docs-api

I'm looking for a way to append new ListItem to particular list in Google Docs API.
My problem is that I can't figure out how to determine last item in list, so I could just insert new item using it's index.
My vision of how to implement this looks like:
body.insertListItem(body.getChildIndex(lastItem) + 1, newItem);
P.S. Am I doing something wrong, maybe there is some other way to implement appending?
Thanks in advance!

A ListItem is a Paragraph that is associated with a list ID. A ListItem may contain Equation, Footnote, HorizontalRule, InlineDrawing, InlineImage, PageBreak, and Text elements. For more information on document structure, see the guide to extending Google Docs.
ListItems with the same list ID belong to the same list and are numbered accordingly. The ListItems for a given list are not required to be adjacent in the document or even have the same parent element. Two items belonging to the same list may exist anywhere in the document while maintaining consecutive numbering, as the following example illustrates:
var body = DocumentApp.getActiveDocument().getBody();
// Append a new list item to the body.
var item1 = body.appendListItem('Item 1');
// Log the new list item's list ID.
Logger.log(item1.getListId());
// Append a table after the list item.
body.appendTable([
['Cell 1', 'Cell 2']
]);
// Append a second list item with the same list ID. The two items are treated as the same list,
// despite not being consecutive.
var item2 = body.appendListItem('Item 2');
item2.setListId(item1);

Related

Dart, compare two lists and return element from first list that does not exist in second list

I have two lists,
List first = [{'name':'ABC','serialNumber':'ABC-124-353'},
{'name':'XYZ','serialNumber':'XYZ-123-567'},
{'name':'GRE', 'serialNumber': 'GRE-290-128'}];
List second = [{'name':'PQR','serialNumber':'PQR-D123-SII23'},{'name':'GAR','serialNumber':'GAR-G43-432'},
{'name':'MNOP','serialNumber':'XYZ-123-567'}];
Is there any easier way to compare first list and second list by serialNumber.
such that element from first list that doesn't exist in second list are outputted as a result.
So in this case
[{'name':'ABC','serialNumber':'ABC-124-353'},{'name':'GRE', 'serialNumber': 'GRE-290-128'}]
from first list is desired output, because ABC-124-353 and GRE-290-128 doesn't exist in list second
Another solution would be to use where on your first List to check if the serialNumber is contained in the second list:
final secondSerials = second.map((item) => item['serialNumber']).toSet();
print(first.where((item) => !secondSerials.contains(item['serialNumber'])).toList());
I'd make a set of the serial numbers of the second list, so that you can do efficient contains checks.
So:
var secondListSerials = {for (var entry in secondList) entry["serialNumber"]};
var firstListOnly = [for (var entry in firstList)
if (!secondListSerials.contains(entry["serialNumber"]) entry
];

How to add same value to a list multiple times in one go in Dart/Flutter

Let's say I have an empty list.
If I want to add a certain number of letters "g" to this list. for example 30 40 etc. the number I send may change.
Is there a way to do this in one go without using a loop?
What is it, if any?
https://api.flutter.dev/flutter/dart-core/List/fillRange.html
I need a method like fillRange.
FillRange does not work on an empty list.
If the list is empty, don't bother using it. Just generate a new list with List.filled:
final list = List.filled(30, 'g');
EDIT: For completeness, here is how to use the above with an existing list:
final list = <String>[...];
// Mutate original list
list.addAll(List.filled(30, 'g'));
// Create new list with spread syntax
final newList = [
...list,
...List.filled(30, 'g'),
];

Creating lists in google sheets

Wasn't really sure how else to word the title, but here's a little more information!
In this spreadsheet: https://docs.google.com/spreadsheets/d/1oXLbc9vkjuWYU60xhsYsT3vv7utOJswVjwJUWS3XpF0/edit?usp=sharing
In the 'Lists by Item Type' sheet, I would like to make lists of all the items separated by item types for use in data validation elsewhere. Is there a function similar to =UNIQUE() that will give me a list of all items with the value x in column Q where x is the item type I input?
Use:
=FILTER(
/* all item names ('Item Database'!$A$3:$A) */,
/* all item types ('Item Database'!$Q$3:$Q) */ = /* the needed item type */
)
This will give you all the items of a given type.

How can I iterate over list items in Pandoc's lua-filter function?

Pandoc's lua filter makes it really easy to iterate over a document and munge the document as you go. My problem is I can't figure out how to isolate list item elements. I can find lists and the block level things inside each list item, but I can't figure out a way to iterate over list items.
For example let's say I had the following Markdown document:
1. One string
Two string
2. Three string
Four string
Lets say I want to make the first line of each list item bold. I can easily change how the paragraphs are handled inside OrderedLists, say using this filter and pandoc --lua-filter=myfilter.lua --to=markdown input.md
local i
OrderedList = function (element)
i = 0
return pandoc.walk_block(element, {
Para = function (element)
i = i + 1
if i == 1 then return pandoc.Para { pandoc.Strong(element.c) }
else return element end
end
})
end
This will indeed change the first paragraph element to bold, but it only changes the first paragraph of the first list item because it's iterating across all paragraphs in all list items in the list, not on each list item, then on each paragraph.
1. **One string**
Two string
2. Three string
Four string
If I separate the two list items into two separate lists again the first paragraph of the first item is caught, but I want to catch the first paragraph of every list item! I can't find anything in the documentation about iterating over list items. How is one supposed to do that?
The pandoc Lua filter docs have recently been updated with more info on the properties of each type. E.g., for OrderedList elements, the docs should say (it currently says items instead of content, which is a bug):
OrderedList
An ordered list.
content: list items (List of Blocks)
listAttributes: list parameters (ListAttributes)
start: alias for listAttributes.start (integer)
style: alias for listAttributes.style (string)
delimiter: alias for listAttributes.delimiter (string)
tag, t: the literal OrderedList (string)
So the easiest way is to iterate over the content field and change items therein:
OrderedList = function (element)
for i, item in ipairs(element.content) do
local first = item[1]
if first and first.t == 'Para' then
element.content[i][1] = pandoc.Para{pandoc.Strong(first.content)}
end
end
return element
end

Trouble adding item to a list with a lookup value

I am adding a new item to a list in SharePoint 2007. One of the columns is a lookup into another list. Here is the code:
li["LOOKUP"] = new SPFieldLookupValue(1,VALUE);
The entry in the list isn't correct and is always using the first value in the other list. When I look at the value of li["LOOKUP"] in the debugger all I get is "1".
VALUE is in the other list and it is the first column.
When adding items to a list only the id of the element from the list being looked up needs to be provided. It was always adding the first element because I always passed in "1". The following code looks up the value and then gets its id and passed this to the field:
SPListItemCollection lookup = LIST.GetItems(qry);
li["LOOKUP"] = lookup[0][SPBuiltInFieldId.ID].ToString();

Resources